file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.undiag | val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv))) | val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv))) | let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 218,
"start_col": 0,
"start_line": 211
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\ | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | wv: Hacl.Impl.Blake2.Core.state_p a m -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.permr_row",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi",
"Hacl.Impl.Blake2.Core.row_p"
] | [] | false | true | false | false | false | let undiag #a #m wv =
| let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get () in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_blocks_st | val blake2_update_blocks_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 77,
"end_line": 883,
"start_col": 0,
"start_line": 873
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_update_blocks",
"Lib.Buffer.op_Brack_Lens_Access"
] | [] | false | false | false | true | true | let blake2_update_blocks_st (al: Spec.alg) (ms: m_spec) =
|
#len: size_t ->
wv: state_p al ms ->
hash: state_p al ms ->
prev: Spec.limb_t al {v prev + v len <= Spec.max_limb al} ->
blocks: lbuffer uint8 len
-> Stack unit
(requires
(fun h ->
live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\
disjoint wv blocks))
(ensures
(fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[| blocks |] (state_v h0 hash))) | false |
|
Vale.SHA.PPC64LE.Rounds.fst | Vale.SHA.PPC64LE.Rounds.va_lemma_Loop_rounds_0_15 | val va_lemma_Loop_rounds_0_15 : va_b0:va_code -> va_s0:va_state -> in_b:buffer128 -> offset:nat ->
k_b:buffer128 -> block:block_w -> hash_orig:hash256 -> input_BE:(seq quad32)
-> Ghost (va_state & va_fuel)
(requires (va_require_total va_b0 (va_code_Loop_rounds_0_15 ()) va_s0 /\ va_get_ok va_s0 /\
(Vale.PPC64LE.Decls.validSrcAddrsOffset128 (va_get_mem_heaplet 0 va_s0) (va_get_reg 4 va_s0)
in_b (offset + 1) 3 (va_get_mem_layout va_s0) Secret /\
Vale.PPC64LE.Decls.validSrcAddrsOffset128 (va_get_mem_heaplet 0 va_s0) (va_get_reg 6 va_s0) k_b
1 4 (va_get_mem_layout va_s0) Secret /\ (let ks = Vale.PPC64LE.Decls.buffer128_as_seq
(va_get_mem_heaplet 0 va_s0) k_b in Vale.SHA.PPC64LE.SHA_helpers.k_reqs ks /\ va_get_reg 4
va_s0 + 48 < pow2_64 /\ va_get_reg 6 va_s0 + 64 < pow2_64 /\ input_BE ==
Vale.Arch.Types.reverse_bytes_quad32_seq (FStar.Seq.Base.slice #Vale.PPC64LE.Machine_s.quad32
(Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_s0) in_b) offset (offset + 4)) /\
block == Vale.SHA.PPC64LE.SHA_helpers.quads_to_block_be input_BE /\ va_get_vec 0 va_s0 ==
FStar.Seq.Base.index #quad32 input_BE 0 /\ (let hash =
Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 0 block hash_orig in l_and (l_and (l_and (l_and
(l_and (l_and (l_and ((va_get_vec 16 va_s0).hi3 == Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32
(FStar.Seq.Base.index #Vale.SHA.PPC64LE.SHA_helpers.word hash 0)) ((va_get_vec 17 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 1))) ((va_get_vec 18 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 2))) ((va_get_vec 19 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 3))) ((va_get_vec 20 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 4))) ((va_get_vec 21 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 5))) ((va_get_vec 22 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 6))) ((va_get_vec 23 va_s0).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 7)) (Vale.SHA.PPC64LE.SHA_helpers.k_index ks 0))) /\
l_and (l_and ((va_get_vec 24 va_s0).hi3 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 1)
((va_get_vec 24 va_s0).hi2 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 2)) ((va_get_vec 24
va_s0).lo1 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 3)))))
(ensures (fun (va_sM, va_fM) -> va_ensure_total va_b0 va_s0 va_sM va_fM /\ va_get_ok va_sM /\
(va_get_reg 6 va_sM == va_get_reg 6 va_s0 + 64 /\ va_get_reg 4 va_sM == va_get_reg 4 va_s0 + 48
/\ (let ks = Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_sM) k_b in (let
next_hash = Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 16 block hash_orig in l_and (l_and
(l_and (l_and (l_and (l_and (l_and ((va_get_vec 16 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 0)) ((va_get_vec 17 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 1))) ((va_get_vec 18 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 2))) ((va_get_vec 19 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 3))) ((va_get_vec 20 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 4))) ((va_get_vec 21 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 5))) ((va_get_vec 22 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 6))) ((va_get_vec 23 va_sM).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 7)) (Vale.SHA.PPC64LE.SHA_helpers.k_index ks 16)))
/\ l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and
(l_and (l_and ((va_get_vec 0 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 16)
((va_get_vec 1 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 1)) ((va_get_vec 2
va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 2)) ((va_get_vec 3 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 3)) ((va_get_vec 4 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 4)) ((va_get_vec 5 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 5)) ((va_get_vec 6 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 6)) ((va_get_vec 7 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 7)) ((va_get_vec 8 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 8)) ((va_get_vec 9 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 9)) ((va_get_vec 10 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 10)) ((va_get_vec 11 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 11)) ((va_get_vec 12 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 12)) ((va_get_vec 13 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 13)) ((va_get_vec 14 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 14)) ((va_get_vec 15 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 15) /\ l_and (l_and ((va_get_vec 24 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 17) ((va_get_vec 24 va_sM).hi2 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 18)) ((va_get_vec 24 va_sM).lo1 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 19))) /\ va_state_eq va_sM (va_update_vec 26 va_sM
(va_update_vec 25 va_sM (va_update_vec 24 va_sM (va_update_vec 23 va_sM (va_update_vec 22 va_sM
(va_update_vec 21 va_sM (va_update_vec 20 va_sM (va_update_vec 19 va_sM (va_update_vec 18 va_sM
(va_update_vec 17 va_sM (va_update_vec 16 va_sM (va_update_vec 15 va_sM (va_update_vec 14 va_sM
(va_update_vec 13 va_sM (va_update_vec 12 va_sM (va_update_vec 11 va_sM (va_update_vec 10 va_sM
(va_update_vec 9 va_sM (va_update_vec 8 va_sM (va_update_vec 7 va_sM (va_update_vec 6 va_sM
(va_update_vec 5 va_sM (va_update_vec 4 va_sM (va_update_vec 3 va_sM (va_update_vec 2 va_sM
(va_update_vec 1 va_sM (va_update_vec 0 va_sM (va_update_reg 6 va_sM (va_update_reg 4 va_sM
(va_update_ok va_sM va_s0)))))))))))))))))))))))))))))))) | val va_lemma_Loop_rounds_0_15 : va_b0:va_code -> va_s0:va_state -> in_b:buffer128 -> offset:nat ->
k_b:buffer128 -> block:block_w -> hash_orig:hash256 -> input_BE:(seq quad32)
-> Ghost (va_state & va_fuel)
(requires (va_require_total va_b0 (va_code_Loop_rounds_0_15 ()) va_s0 /\ va_get_ok va_s0 /\
(Vale.PPC64LE.Decls.validSrcAddrsOffset128 (va_get_mem_heaplet 0 va_s0) (va_get_reg 4 va_s0)
in_b (offset + 1) 3 (va_get_mem_layout va_s0) Secret /\
Vale.PPC64LE.Decls.validSrcAddrsOffset128 (va_get_mem_heaplet 0 va_s0) (va_get_reg 6 va_s0) k_b
1 4 (va_get_mem_layout va_s0) Secret /\ (let ks = Vale.PPC64LE.Decls.buffer128_as_seq
(va_get_mem_heaplet 0 va_s0) k_b in Vale.SHA.PPC64LE.SHA_helpers.k_reqs ks /\ va_get_reg 4
va_s0 + 48 < pow2_64 /\ va_get_reg 6 va_s0 + 64 < pow2_64 /\ input_BE ==
Vale.Arch.Types.reverse_bytes_quad32_seq (FStar.Seq.Base.slice #Vale.PPC64LE.Machine_s.quad32
(Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_s0) in_b) offset (offset + 4)) /\
block == Vale.SHA.PPC64LE.SHA_helpers.quads_to_block_be input_BE /\ va_get_vec 0 va_s0 ==
FStar.Seq.Base.index #quad32 input_BE 0 /\ (let hash =
Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 0 block hash_orig in l_and (l_and (l_and (l_and
(l_and (l_and (l_and ((va_get_vec 16 va_s0).hi3 == Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32
(FStar.Seq.Base.index #Vale.SHA.PPC64LE.SHA_helpers.word hash 0)) ((va_get_vec 17 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 1))) ((va_get_vec 18 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 2))) ((va_get_vec 19 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 3))) ((va_get_vec 20 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 4))) ((va_get_vec 21 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 5))) ((va_get_vec 22 va_s0).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 6))) ((va_get_vec 23 va_s0).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word hash 7)) (Vale.SHA.PPC64LE.SHA_helpers.k_index ks 0))) /\
l_and (l_and ((va_get_vec 24 va_s0).hi3 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 1)
((va_get_vec 24 va_s0).hi2 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 2)) ((va_get_vec 24
va_s0).lo1 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 3)))))
(ensures (fun (va_sM, va_fM) -> va_ensure_total va_b0 va_s0 va_sM va_fM /\ va_get_ok va_sM /\
(va_get_reg 6 va_sM == va_get_reg 6 va_s0 + 64 /\ va_get_reg 4 va_sM == va_get_reg 4 va_s0 + 48
/\ (let ks = Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_sM) k_b in (let
next_hash = Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 16 block hash_orig in l_and (l_and
(l_and (l_and (l_and (l_and (l_and ((va_get_vec 16 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 0)) ((va_get_vec 17 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 1))) ((va_get_vec 18 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 2))) ((va_get_vec 19 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 3))) ((va_get_vec 20 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 4))) ((va_get_vec 21 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 5))) ((va_get_vec 22 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 6))) ((va_get_vec 23 va_sM).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 7)) (Vale.SHA.PPC64LE.SHA_helpers.k_index ks 16)))
/\ l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and
(l_and (l_and ((va_get_vec 0 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 16)
((va_get_vec 1 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 1)) ((va_get_vec 2
va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 2)) ((va_get_vec 3 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 3)) ((va_get_vec 4 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 4)) ((va_get_vec 5 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 5)) ((va_get_vec 6 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 6)) ((va_get_vec 7 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 7)) ((va_get_vec 8 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 8)) ((va_get_vec 9 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 9)) ((va_get_vec 10 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 10)) ((va_get_vec 11 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 11)) ((va_get_vec 12 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 12)) ((va_get_vec 13 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 13)) ((va_get_vec 14 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 14)) ((va_get_vec 15 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 15) /\ l_and (l_and ((va_get_vec 24 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 17) ((va_get_vec 24 va_sM).hi2 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 18)) ((va_get_vec 24 va_sM).lo1 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 19))) /\ va_state_eq va_sM (va_update_vec 26 va_sM
(va_update_vec 25 va_sM (va_update_vec 24 va_sM (va_update_vec 23 va_sM (va_update_vec 22 va_sM
(va_update_vec 21 va_sM (va_update_vec 20 va_sM (va_update_vec 19 va_sM (va_update_vec 18 va_sM
(va_update_vec 17 va_sM (va_update_vec 16 va_sM (va_update_vec 15 va_sM (va_update_vec 14 va_sM
(va_update_vec 13 va_sM (va_update_vec 12 va_sM (va_update_vec 11 va_sM (va_update_vec 10 va_sM
(va_update_vec 9 va_sM (va_update_vec 8 va_sM (va_update_vec 7 va_sM (va_update_vec 6 va_sM
(va_update_vec 5 va_sM (va_update_vec 4 va_sM (va_update_vec 3 va_sM (va_update_vec 2 va_sM
(va_update_vec 1 va_sM (va_update_vec 0 va_sM (va_update_reg 6 va_sM (va_update_reg 4 va_sM
(va_update_ok va_sM va_s0)))))))))))))))))))))))))))))))) | let va_lemma_Loop_rounds_0_15 va_b0 va_s0 in_b offset k_b block hash_orig input_BE =
let (va_mods:va_mods_t) = [va_Mod_vec 26; va_Mod_vec 25; va_Mod_vec 24; va_Mod_vec 23; va_Mod_vec
22; va_Mod_vec 21; va_Mod_vec 20; va_Mod_vec 19; va_Mod_vec 18; va_Mod_vec 17; va_Mod_vec 16;
va_Mod_vec 15; va_Mod_vec 14; va_Mod_vec 13; va_Mod_vec 12; va_Mod_vec 11; va_Mod_vec 10;
va_Mod_vec 9; va_Mod_vec 8; va_Mod_vec 7; va_Mod_vec 6; va_Mod_vec 5; va_Mod_vec 4; va_Mod_vec
3; va_Mod_vec 2; va_Mod_vec 1; va_Mod_vec 0; va_Mod_reg 6; va_Mod_reg 4; va_Mod_ok] in
let va_qc = va_qcode_Loop_rounds_0_15 va_mods in_b offset k_b block hash_orig input_BE in
let (va_sM, va_fM, va_g) = va_wp_sound_code_norm (va_code_Loop_rounds_0_15 ()) va_qc va_s0 (fun
va_s0 va_sM va_g -> let () = va_g in label va_range1
"***** POSTCONDITION NOT MET AT line 56 column 1 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_ok va_sM) /\ (label va_range1
"***** POSTCONDITION NOT MET AT line 95 column 29 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_reg 6 va_sM == va_get_reg 6 va_s0 + 64) /\ label va_range1
"***** POSTCONDITION NOT MET AT line 96 column 29 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_reg 4 va_sM == va_get_reg 4 va_s0 + 48) /\ label va_range1
"***** POSTCONDITION NOT MET AT line 97 column 9 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(let ks = Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_sM) k_b in label
va_range1
"***** POSTCONDITION NOT MET AT line 106 column 85 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(let next_hash = Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 16 block hash_orig in l_and
(l_and (l_and (l_and (l_and (l_and (l_and ((va_get_vec 16 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 0)) ((va_get_vec 17 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 1))) ((va_get_vec 18 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 2))) ((va_get_vec 19 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 3))) ((va_get_vec 20 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 4))) ((va_get_vec 21 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 5))) ((va_get_vec 22 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 6))) ((va_get_vec 23 va_sM).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word next_hash 7)) (Vale.SHA.PPC64LE.SHA_helpers.k_index ks 16)))
/\ label va_range1
"***** POSTCONDITION NOT MET AT line 112 column 40 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and
(l_and (l_and ((va_get_vec 0 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 16)
((va_get_vec 1 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 1)) ((va_get_vec 2
va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 2)) ((va_get_vec 3 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 3)) ((va_get_vec 4 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 4)) ((va_get_vec 5 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 5)) ((va_get_vec 6 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 6)) ((va_get_vec 7 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 7)) ((va_get_vec 8 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 8)) ((va_get_vec 9 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 9)) ((va_get_vec 10 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 10)) ((va_get_vec 11 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 11)) ((va_get_vec 12 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 12)) ((va_get_vec 13 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 13)) ((va_get_vec 14 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 14)) ((va_get_vec 15 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 15)) /\ label va_range1
"***** POSTCONDITION NOT MET AT line 113 column 92 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(l_and (l_and ((va_get_vec 24 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 17)
((va_get_vec 24 va_sM).hi2 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 18)) ((va_get_vec 24
va_sM).lo1 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 19))))) in
assert_norm (va_qc.mods == va_mods);
va_lemma_norm_mods ([va_Mod_vec 26; va_Mod_vec 25; va_Mod_vec 24; va_Mod_vec 23; va_Mod_vec 22;
va_Mod_vec 21; va_Mod_vec 20; va_Mod_vec 19; va_Mod_vec 18; va_Mod_vec 17; va_Mod_vec 16;
va_Mod_vec 15; va_Mod_vec 14; va_Mod_vec 13; va_Mod_vec 12; va_Mod_vec 11; va_Mod_vec 10;
va_Mod_vec 9; va_Mod_vec 8; va_Mod_vec 7; va_Mod_vec 6; va_Mod_vec 5; va_Mod_vec 4; va_Mod_vec
3; va_Mod_vec 2; va_Mod_vec 1; va_Mod_vec 0; va_Mod_reg 6; va_Mod_reg 4; va_Mod_ok]) va_sM
va_s0;
(va_sM, va_fM) | {
"file_name": "obj/Vale.SHA.PPC64LE.Rounds.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 16,
"end_line": 179,
"start_col": 0,
"start_line": 112
} | module Vale.SHA.PPC64LE.Rounds
open Vale.Def.Opaque_s
open Vale.Def.Types_s
open Vale.Def.Words_s
open Vale.Def.Words.Seq_s
open FStar.Seq
open Vale.Arch.Types
open Vale.Arch.HeapImpl
open Vale.PPC64LE.Machine_s
open Vale.PPC64LE.Memory
open Vale.PPC64LE.Stack_i
open Vale.PPC64LE.State
open Vale.PPC64LE.Decls
open Vale.PPC64LE.QuickCode
open Vale.PPC64LE.QuickCodes
open Vale.PPC64LE.InsBasic
open Vale.PPC64LE.InsMem
open Vale.PPC64LE.InsStack
open Vale.PPC64LE.InsVector
open Vale.SHA.PPC64LE.SHA_helpers
open Spec.SHA2
open Spec.Agile.Hash
open Spec.Hash.Definitions
open Spec.Loops
open Vale.SHA.PPC64LE.Rounds.Core
open Vale.SHA2.Wrapper
#reset-options "--z3rlimit 2000"
//-- Loop_rounds_0_15
[@ "opaque_to_smt" va_qattr]
let va_code_Loop_rounds_0_15 () =
(va_Block (va_CCons (va_code_Loop_rounds_3_7_11_body 3 (va_op_vec_opr_vec 4)) (va_CCons
(va_code_Loop_rounds_3_7_11_body 7 (va_op_vec_opr_vec 8)) (va_CCons
(va_code_Loop_rounds_3_7_11_body 11 (va_op_vec_opr_vec 12)) (va_CCons (va_Block (va_CNil ()))
(va_CCons (va_Block (va_CNil ())) (va_CCons (va_Block (va_CNil ())) (va_CCons
(va_code_Loop_rounds_1_3 ()) (va_CCons (va_code_Loop_rounds_0_59_a 0) (va_CCons
(va_code_Loop_rounds_5_7 ()) (va_CCons (va_code_Loop_rounds_0_59_b 4) (va_CCons
(va_code_Loop_rounds_9_11 ()) (va_CCons (va_code_Loop_rounds_0_59_c 8) (va_CCons
(va_code_Loop_rounds_13_15 ()) (va_CCons (va_code_Loop_rounds_0_59_d 12) (va_CCons
(va_code_Loop_rounds_16_63_body 16 (va_op_vec_opr_vec 0) (va_op_vec_opr_vec 1)
(va_op_vec_opr_vec 9) (va_op_vec_opr_vec 14)) (va_CNil ())))))))))))))))))
[@ "opaque_to_smt" va_qattr]
let va_codegen_success_Loop_rounds_0_15 () =
(va_pbool_and (va_codegen_success_Loop_rounds_3_7_11_body 3 (va_op_vec_opr_vec 4)) (va_pbool_and
(va_codegen_success_Loop_rounds_3_7_11_body 7 (va_op_vec_opr_vec 8)) (va_pbool_and
(va_codegen_success_Loop_rounds_3_7_11_body 11 (va_op_vec_opr_vec 12)) (va_pbool_and
(va_codegen_success_Loop_rounds_1_3 ()) (va_pbool_and (va_codegen_success_Loop_rounds_0_59_a 0)
(va_pbool_and (va_codegen_success_Loop_rounds_5_7 ()) (va_pbool_and
(va_codegen_success_Loop_rounds_0_59_b 4) (va_pbool_and (va_codegen_success_Loop_rounds_9_11
()) (va_pbool_and (va_codegen_success_Loop_rounds_0_59_c 8) (va_pbool_and
(va_codegen_success_Loop_rounds_13_15 ()) (va_pbool_and (va_codegen_success_Loop_rounds_0_59_d
12) (va_pbool_and (va_codegen_success_Loop_rounds_16_63_body 16 (va_op_vec_opr_vec 0)
(va_op_vec_opr_vec 1) (va_op_vec_opr_vec 9) (va_op_vec_opr_vec 14)) (va_ttrue ())))))))))))))
[@ "opaque_to_smt" va_qattr]
let va_qcode_Loop_rounds_0_15 (va_mods:va_mods_t) (in_b:buffer128) (offset:nat) (k_b:buffer128)
(block:block_w) (hash_orig:hash256) (input_BE:(seq quad32)) : (va_quickCode unit
(va_code_Loop_rounds_0_15 ())) =
(qblock va_mods (fun (va_s:va_state) -> let (va_old_s:va_state) = va_s in let
(va_arg34:(FStar.Seq.Base.seq Vale.Def.Types_s.quad32)) = input_BE in va_qPURE va_range1
"***** PRECONDITION NOT MET AT line 115 column 28 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(fun (_:unit) -> Vale.SHA.PPC64LE.SHA_helpers.lemma_quads_to_block_be va_arg34) (va_QSeq
va_range1
"***** PRECONDITION NOT MET AT line 117 column 28 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_3_7_11_body 3 (va_op_vec_opr_vec 4) in_b (offset + 1)) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 118 column 28 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_3_7_11_body 7 (va_op_vec_opr_vec 8) in_b (offset + 2)) (va_QBind
va_range1
"***** PRECONDITION NOT MET AT line 119 column 28 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_3_7_11_body 11 (va_op_vec_opr_vec 12) in_b (offset + 3)) (fun
(va_s:va_state) _ -> va_qAssertSquash va_range1
"***** EXPRESSION PRECONDITIONS NOT MET WITHIN line 120 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
((fun a_539 (s_540:(FStar.Seq.Base.seq a_539)) (i_541:Prims.nat) -> let (i_515:Prims.nat) =
i_541 in Prims.b2t (Prims.op_LessThan i_515 (FStar.Seq.Base.length #a_539 s_540))) quad32
input_BE 1) (fun _ -> va_qAssert va_range1
"***** PRECONDITION NOT MET AT line 120 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_vec 4 va_s == FStar.Seq.Base.index #quad32 input_BE 1) (va_qAssertSquash va_range1
"***** EXPRESSION PRECONDITIONS NOT MET WITHIN line 121 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
((fun a_539 (s_540:(FStar.Seq.Base.seq a_539)) (i_541:Prims.nat) -> let (i_515:Prims.nat) =
i_541 in Prims.b2t (Prims.op_LessThan i_515 (FStar.Seq.Base.length #a_539 s_540))) quad32
input_BE 2) (fun _ -> va_qAssert va_range1
"***** PRECONDITION NOT MET AT line 121 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_vec 8 va_s == FStar.Seq.Base.index #quad32 input_BE 2) (va_qAssertSquash va_range1
"***** EXPRESSION PRECONDITIONS NOT MET WITHIN line 122 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
((fun a_539 (s_540:(FStar.Seq.Base.seq a_539)) (i_541:Prims.nat) -> let (i_515:Prims.nat) =
i_541 in Prims.b2t (Prims.op_LessThan i_515 (FStar.Seq.Base.length #a_539 s_540))) quad32
input_BE 3) (fun _ -> va_qAssert va_range1
"***** PRECONDITION NOT MET AT line 122 column 5 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_vec 12 va_s == FStar.Seq.Base.index #quad32 input_BE 3) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 124 column 20 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_1_3 block) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 125 column 23 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_0_59_a 0 k_b block hash_orig) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 127 column 20 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_5_7 block) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 128 column 23 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_0_59_b 4 k_b block hash_orig) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 130 column 21 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_9_11 block) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 131 column 23 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_0_59_c 8 k_b block hash_orig) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 133 column 22 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_13_15 block) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 134 column 23 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_0_59_d 12 k_b block hash_orig) (va_QSeq va_range1
"***** PRECONDITION NOT MET AT line 136 column 27 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_quick_Loop_rounds_16_63_body 16 (va_op_vec_opr_vec 0) (va_op_vec_opr_vec 1)
(va_op_vec_opr_vec 9) (va_op_vec_opr_vec 14) block) (va_QEmpty (())))))))))))))))))))))) | {
"checked_file": "/",
"dependencies": [
"Vale.SHA2.Wrapper.fsti.checked",
"Vale.SHA.PPC64LE.SHA_helpers.fsti.checked",
"Vale.SHA.PPC64LE.Rounds.Core.fsti.checked",
"Vale.PPC64LE.State.fsti.checked",
"Vale.PPC64LE.Stack_i.fsti.checked",
"Vale.PPC64LE.QuickCodes.fsti.checked",
"Vale.PPC64LE.QuickCode.fst.checked",
"Vale.PPC64LE.Memory.fsti.checked",
"Vale.PPC64LE.Machine_s.fst.checked",
"Vale.PPC64LE.InsVector.fsti.checked",
"Vale.PPC64LE.InsStack.fsti.checked",
"Vale.PPC64LE.InsMem.fsti.checked",
"Vale.PPC64LE.InsBasic.fsti.checked",
"Vale.PPC64LE.Decls.fsti.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Words.Seq_s.fsti.checked",
"Vale.Def.Types_s.fst.checked",
"Vale.Def.Opaque_s.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.Arch.HeapImpl.fsti.checked",
"Spec.SHA2.fsti.checked",
"Spec.Loops.fst.checked",
"Spec.Hash.Definitions.fst.checked",
"Spec.Agile.Hash.fsti.checked",
"prims.fst.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.SHA.PPC64LE.Rounds.fst"
} | [
{
"abbrev": false,
"full_module": "Vale.SHA2.Wrapper",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.PPC64LE.Rounds.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Loops",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Hash.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Agile.Hash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.SHA2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.PPC64LE.SHA_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.InsVector",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.InsStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.InsMem",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.InsBasic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.QuickCodes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.QuickCode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.Decls",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.State",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.Stack_i",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.PPC64LE.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.HeapImpl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Words.Seq_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Words_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Types_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Opaque_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.PPC64LE",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.PPC64LE",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 2000,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
va_b0: Vale.PPC64LE.Decls.va_code ->
va_s0: Vale.PPC64LE.Decls.va_state ->
in_b: Vale.PPC64LE.Memory.buffer128 ->
offset: Prims.nat ->
k_b: Vale.PPC64LE.Memory.buffer128 ->
block: Vale.SHA.PPC64LE.SHA_helpers.block_w ->
hash_orig: Vale.SHA.PPC64LE.SHA_helpers.hash256 ->
input_BE: FStar.Seq.Base.seq Vale.PPC64LE.Memory.quad32
-> Prims.Ghost (Vale.PPC64LE.Decls.va_state * Vale.PPC64LE.Decls.va_fuel) | Prims.Ghost | [] | [] | [
"Vale.PPC64LE.Decls.va_code",
"Vale.PPC64LE.Decls.va_state",
"Vale.PPC64LE.Memory.buffer128",
"Prims.nat",
"Vale.SHA.PPC64LE.SHA_helpers.block_w",
"Vale.SHA.PPC64LE.SHA_helpers.hash256",
"FStar.Seq.Base.seq",
"Vale.PPC64LE.Memory.quad32",
"Vale.PPC64LE.QuickCodes.fuel",
"Prims.unit",
"FStar.Pervasives.Native.Mktuple2",
"Vale.PPC64LE.Decls.va_fuel",
"Vale.PPC64LE.QuickCode.va_lemma_norm_mods",
"Prims.Cons",
"Vale.PPC64LE.QuickCode.mod_t",
"Vale.PPC64LE.QuickCode.va_Mod_vec",
"Vale.PPC64LE.QuickCode.va_Mod_reg",
"Vale.PPC64LE.QuickCode.va_Mod_ok",
"Prims.Nil",
"FStar.Pervasives.assert_norm",
"Prims.eq2",
"Prims.list",
"Vale.PPC64LE.QuickCode.__proj__QProc__item__mods",
"Vale.SHA.PPC64LE.Rounds.va_code_Loop_rounds_0_15",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.tuple3",
"Vale.PPC64LE.Machine_s.state",
"Vale.PPC64LE.QuickCodes.va_wp_sound_code_norm",
"Prims.l_and",
"Vale.PPC64LE.QuickCodes.label",
"Vale.PPC64LE.QuickCodes.va_range1",
"Prims.b2t",
"Vale.PPC64LE.Decls.va_get_ok",
"Prims.int",
"Vale.PPC64LE.Decls.va_get_reg",
"Prims.op_Addition",
"Vale.Def.Words_s.nat32",
"Vale.Def.Words_s.__proj__Mkfour__item__hi3",
"Vale.Def.Types_s.nat32",
"Vale.PPC64LE.Decls.va_get_vec",
"Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32",
"FStar.Seq.Base.index",
"Vale.SHA.PPC64LE.SHA_helpers.word",
"Vale.Arch.Types.add_wrap32",
"Vale.SHA.PPC64LE.SHA_helpers.k_index",
"Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale",
"Vale.SHA.PPC64LE.SHA_helpers.ws_opaque",
"Vale.Def.Words_s.__proj__Mkfour__item__hi2",
"Vale.Def.Words_s.__proj__Mkfour__item__lo1",
"Vale.Def.Types_s.quad32",
"Vale.PPC64LE.Decls.buffer128_as_seq",
"Vale.PPC64LE.Decls.va_get_mem_heaplet",
"Vale.PPC64LE.QuickCode.quickCode",
"Vale.SHA.PPC64LE.Rounds.va_qcode_Loop_rounds_0_15"
] | [] | false | false | false | false | false | let va_lemma_Loop_rounds_0_15 va_b0 va_s0 in_b offset k_b block hash_orig input_BE =
| let va_mods:va_mods_t =
[
va_Mod_vec 26; va_Mod_vec 25; va_Mod_vec 24; va_Mod_vec 23; va_Mod_vec 22; va_Mod_vec 21;
va_Mod_vec 20; va_Mod_vec 19; va_Mod_vec 18; va_Mod_vec 17; va_Mod_vec 16; va_Mod_vec 15;
va_Mod_vec 14; va_Mod_vec 13; va_Mod_vec 12; va_Mod_vec 11; va_Mod_vec 10; va_Mod_vec 9;
va_Mod_vec 8; va_Mod_vec 7; va_Mod_vec 6; va_Mod_vec 5; va_Mod_vec 4; va_Mod_vec 3; va_Mod_vec 2;
va_Mod_vec 1; va_Mod_vec 0; va_Mod_reg 6; va_Mod_reg 4; va_Mod_ok
]
in
let va_qc = va_qcode_Loop_rounds_0_15 va_mods in_b offset k_b block hash_orig input_BE in
let va_sM, va_fM, va_g =
va_wp_sound_code_norm (va_code_Loop_rounds_0_15 ())
va_qc
va_s0
(fun va_s0 va_sM va_g ->
let () = va_g in
label va_range1
"***** POSTCONDITION NOT MET AT line 56 column 1 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_ok va_sM) /\
(label va_range1
"***** POSTCONDITION NOT MET AT line 95 column 29 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_reg 6 va_sM == va_get_reg 6 va_s0 + 64) /\
label va_range1
"***** POSTCONDITION NOT MET AT line 96 column 29 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(va_get_reg 4 va_sM == va_get_reg 4 va_s0 + 48) /\
label va_range1
"***** POSTCONDITION NOT MET AT line 97 column 9 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(let ks = Vale.PPC64LE.Decls.buffer128_as_seq (va_get_mem_heaplet 0 va_sM) k_b in
label va_range1
"***** POSTCONDITION NOT MET AT line 106 column 85 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(let next_hash =
Vale.SHA.PPC64LE.SHA_helpers.repeat_range_vale 16 block hash_orig
in
l_and (l_and (l_and (l_and (l_and (l_and (l_and ((va_get_vec 16 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
0))
((va_get_vec 17 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
1)))
((va_get_vec 18 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
2)))
((va_get_vec 19 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
3)))
((va_get_vec 20 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index #Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
4)))
((va_get_vec 21 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index #Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
5)))
((va_get_vec 22 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index #Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
6)))
((va_get_vec 23 va_sM).hi3 ==
Vale.Arch.Types.add_wrap32 (Vale.SHA.PPC64LE.SHA_helpers.word_to_nat32 (FStar.Seq.Base.index
#Vale.SHA.PPC64LE.SHA_helpers.word
next_hash
7))
(Vale.SHA.PPC64LE.SHA_helpers.k_index ks 16))) /\
label va_range1
"***** POSTCONDITION NOT MET AT line 112 column 40 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (l_and (
l_and (l_and (l_and ((va_get_vec 0
va_sM)
.hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
16)
((va_get_vec 1 va_sM)
.hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
1))
((va_get_vec 2 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
2))
((va_get_vec 3 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
3))
((va_get_vec 4 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
4))
((va_get_vec 5 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque
block
5))
((va_get_vec 6 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block
6))
((va_get_vec 7 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 7
))
((va_get_vec 8 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 8))
((va_get_vec 9 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 9))
((va_get_vec 10 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 10))
((va_get_vec 11 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 11))
((va_get_vec 12 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 12))
((va_get_vec 13 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 13))
((va_get_vec 14 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 14))
((va_get_vec 15 va_sM).hi3 == Vale.SHA.PPC64LE.SHA_helpers.ws_opaque block 15)) /\
label va_range1
"***** POSTCONDITION NOT MET AT line 113 column 92 of file /home/gebner/fstar_dataset/projects/hacl-star/vale/code/thirdPartyPorts/OpenSSL/sha/Vale.SHA.PPC64LE.Rounds.vaf *****"
(l_and (l_and ((va_get_vec 24 va_sM).hi3 ==
Vale.SHA.PPC64LE.SHA_helpers.k_index ks 17)
((va_get_vec 24 va_sM).hi2 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 18))
((va_get_vec 24 va_sM).lo1 == Vale.SHA.PPC64LE.SHA_helpers.k_index ks 19)))))
in
assert_norm (va_qc.mods == va_mods);
va_lemma_norm_mods ([
va_Mod_vec 26; va_Mod_vec 25; va_Mod_vec 24; va_Mod_vec 23; va_Mod_vec 22; va_Mod_vec 21;
va_Mod_vec 20; va_Mod_vec 19; va_Mod_vec 18; va_Mod_vec 17; va_Mod_vec 16; va_Mod_vec 15;
va_Mod_vec 14; va_Mod_vec 13; va_Mod_vec 12; va_Mod_vec 11; va_Mod_vec 10; va_Mod_vec 9;
va_Mod_vec 8; va_Mod_vec 7; va_Mod_vec 6; va_Mod_vec 5; va_Mod_vec 4; va_Mod_vec 3;
va_Mod_vec 2; va_Mod_vec 1; va_Mod_vec 0; va_Mod_reg 6; va_Mod_reg 4; va_Mod_ok
])
va_sM
va_s0;
(va_sM, va_fM) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_compress2 | val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m))) | val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m))) | let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 387,
"start_col": 0,
"start_line": 374
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m))) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | wv: Hacl.Impl.Blake2.Core.state_p al ms -> m: Hacl.Impl.Blake2.Core.block_w al
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.block_w",
"Lib.Buffer.loop_refl",
"Hacl.Impl.Blake2.Generic.rounds_t",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Blake2.Generic.blake2_round",
"Prims.unit",
"Lib.LoopCombinators.unfold_repeati",
"Lib.Sequence.lseq",
"Lib.IntTypes.int_t",
"Spec.Blake2.Definitions.wt",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.rounds",
"Hacl.Impl.Blake2.Core.state_v",
"FStar.Monotonic.HyperStack.mem",
"Prims.nat",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"Prims.pow2",
"Spec.Blake2.blake2_round",
"Lib.Buffer.op_Brack_Lens_Access",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.word_t",
"FStar.UInt32.__uint_to_t",
"FStar.Ghost.erased",
"LowStar.Monotonic.Buffer.loc",
"FStar.Ghost.hide",
"Lib.Buffer.loc",
"Hacl.Impl.Blake2.Core.element_t",
"Spec.Blake2.Definitions.state",
"FStar.HyperStack.ST.get"
] | [] | false | true | false | false | false | let blake2_compress2 #al #ms wv m =
| let h0 = ST.get () in
[@@ inline_let ]let a_spec = Spec.state al in
[@@ inline_let ]let refl h = state_v h wv in
[@@ inline_let ]let footprint = Ghost.hide (loc wv) in
[@@ inline_let ]let spec h = Spec.blake2_round al h.[| m |] in
loop_refl h0
(rounds_t al)
a_spec
refl
footprint
spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.diag | val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv))) | val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv))) | let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 18,
"end_line": 202,
"start_col": 0,
"start_line": 195
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\ | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | wv: Hacl.Impl.Blake2.Core.state_p a m -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.permr_row",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi",
"Hacl.Impl.Blake2.Core.row_p"
] | [] | false | true | false | false | false | let diag #a #m wv =
| let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get () in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.get_sigma' | val get_sigma' (start: size_t{v start <= 144}) (i: size_t{normalize (i <=. 15ul)})
: Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[ v start + v i ]))) | val get_sigma' (start: size_t{v start <= 144}) (i: size_t{normalize (i <=. 15ul)})
: Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[ v start + v i ]))) | let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 234,
"start_col": 0,
"start_line": 228
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start))) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
start: Lib.IntTypes.size_t{Lib.IntTypes.v start <= 144} ->
i: Lib.IntTypes.size_t{FStar.Pervasives.normalize (i <=. 15ul)}
-> FStar.HyperStack.ST.Stack Spec.Blake2.Definitions.sigma_elt_t | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"FStar.Pervasives.normalize",
"Lib.IntTypes.op_Less_Equals_Dot",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Blake2.Generic.get_sigma",
"Lib.IntTypes.op_Plus_Bang",
"Spec.Blake2.Definitions.sigma_elt_t",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_True",
"Prims.l_and",
"Prims.eq2",
"Lib.Sequence.op_String_Access",
"Spec.Blake2.Definitions.size_sigmaTable",
"Spec.Blake2.sigmaTable",
"Prims.op_Addition"
] | [] | false | true | false | false | false | let get_sigma' (start: size_t{v start <= 144}) (i: size_t{normalize (i <=. 15ul)})
: Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[ v start + v i ]))) =
| get_sigma (start +! i) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update1_st | val blake2_update1_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 113,
"end_line": 491,
"start_col": 0,
"start_line": 481
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.op_LessThan",
"Prims.op_Division",
"Lib.Buffer.length",
"Lib.Buffer.MUT",
"Spec.Blake2.Definitions.size_block",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Lib.Buffer.live",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_update1",
"Lib.Buffer.op_Brack_Lens_Access"
] | [] | false | false | false | true | true | let blake2_update1_st (al: Spec.alg) (ms: m_spec) =
|
#len: size_t ->
wv: state_p al ms ->
hash: state_p al ms ->
prev: Spec.limb_t al {v prev + v len <= Spec.max_limb al} ->
d: lbuffer uint8 len ->
i: size_t{v i < length d / Spec.size_block al}
-> Stack unit
(requires
(fun h ->
live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\
disjoint wv d))
(ensures
(fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[| d |] (v i) (state_v h0 hash))) | false |
|
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.gather | val gather (#a:Type)
(#uses:_)
(#p0 p1:perm)
(#v0 #v1:erased a)
(r:ref a)
: STGhost unit uses
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r (sum_perm p0 p1) v0)
(requires True)
(ensures fun _ -> v0 == v1) | val gather (#a:Type)
(#uses:_)
(#p0 p1:perm)
(#v0 #v1:erased a)
(r:ref a)
: STGhost unit uses
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r (sum_perm p0 p1) v0)
(requires True)
(ensures fun _ -> v0 == v1) | let gather (#a:Type)
(#uses:_)
(#p0 p1:perm)
(#v0 #v1:erased a)
(r:ref a)
: STGhost unit uses
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r (sum_perm p0 p1) v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost (fun _ -> R.gather #a #uses #p0 #p1 #v0 #v1 r) | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 63,
"end_line": 188,
"start_col": 0,
"start_line": 178
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v
let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r)
let pts_to_not_null #a #opened #p #v r
= extract_fact #opened (pts_to r p v) (r =!= null) (R.pts_to_not_null r p v);
()
let alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= let r = coerce_steel (fun _ -> R.alloc x) in
r
let read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun _ -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v)
= let u = coerce_steel (fun _ -> R.read r) in
return u
let write (#a:Type)
(#v:erased a)
(r:ref a)
(x:a)
: STT unit
(pts_to r full_perm v)
(fun _ -> pts_to r full_perm x)
= coerce_steel (fun _ -> R.write r x);
return ()
let free (#a:Type)
(#v:erased a)
(r:ref a)
: STT unit
(pts_to r full_perm v)
(fun _ -> emp)
= coerce_steel(fun _ -> R.free r);
return ()
/// Local primitive, to be extracted to Low* EPushFrame. To remember
/// that we need to call some pop_frame later, we insert some dummy
/// vprop into the context.
let _stack_frame : vprop = pure True
let _push_frame () : STT unit emp (fun _ -> _stack_frame) =
rewrite (pure True) _stack_frame
/// Local primitive, to be extracted to Low* EBufCreate
let _alloca (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= alloc x
/// Local primitive, to be extracted to Low* EPopFrame
let _free_and_pop_frame
(#a:Type)
(#v:erased a)
(r:ref a)
: STT unit
(pts_to r full_perm v `star` _stack_frame)
(fun _ -> emp)
= free r;
rewrite _stack_frame (pure True);
elim_pure _
let with_local
(#t: Type)
(init: t)
(#pre: vprop)
(#ret_t: Type)
(#post: ret_t -> vprop)
(body: (r: ref t) ->
STT ret_t
(pts_to r full_perm init `star` pre)
(fun v -> exists_ (pts_to r full_perm) `star` post v)
)
: STF ret_t pre post True (fun _ -> True)
= _push_frame ();
let r = _alloca init in
let v = body r in
let _ = elim_exists () in
_free_and_pop_frame r;
return v
let with_named_local
(#t: Type)
(init: t)
(#pre: vprop)
(#ret_t: Type)
(#post: ret_t -> vprop)
(name: string)
(body: (r: ref t) ->
STT ret_t
(pts_to r full_perm init `star` pre)
(fun v -> exists_ (pts_to r full_perm) `star` post v)
)
: STF ret_t pre post True (fun _ -> True)
= _push_frame ();
[@(rename_let name)]
let r = _alloca init in
let v = body r in
let _ = elim_exists () in
_free_and_pop_frame r;
return v
let share (#a:Type)
(#uses:_)
(#p:perm)
(#v:erased a)
(r:ref a)
: STGhostT unit uses
(pts_to r p v)
(fun _ -> pts_to r (half_perm p) v `star` pts_to r (half_perm p) v)
= coerce_ghost (fun _ -> R.share r) | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p1: Steel.FractionalPermission.perm -> r: Steel.ST.HigherReference.ref a
-> Steel.ST.Effect.Ghost.STGhost Prims.unit | Steel.ST.Effect.Ghost.STGhost | [] | [] | [
"Steel.Memory.inames",
"Steel.FractionalPermission.perm",
"FStar.Ghost.erased",
"Steel.ST.HigherReference.ref",
"Steel.ST.Coercions.coerce_ghost",
"Prims.unit",
"Steel.Effect.Common.star",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"FStar.Ghost.reveal",
"Steel.FractionalPermission.sum_perm",
"Steel.Effect.Common.vprop",
"Prims.l_True",
"Prims.eq2",
"Steel.HigherReference.gather",
"Steel.ST.HigherReference.pts_to"
] | [] | false | true | false | false | false | let gather (#a: Type) (#uses: _) (#p0 p1: perm) (#v0 #v1: erased a) (r: ref a)
: STGhost unit
uses
((pts_to r p0 v0) `star` (pts_to r p1 v1))
(fun _ -> pts_to r (sum_perm p0 p1) v0)
(requires True)
(ensures fun _ -> v0 == v1) =
| coerce_ghost (fun _ -> R.gather #a #uses #p0 #p1 #v0 #v1 r) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_block | val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms | val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms | let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 41,
"end_line": 478,
"start_col": 0,
"start_line": 477
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Hacl.Impl.Blake2.Generic.blake2_update_block_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Prims.bool",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.max_limb",
"Hacl.Impl.Blake2.Core.block_p",
"Hacl.Impl.Blake2.Generic.blake2_compress",
"Prims.unit"
] | [] | false | false | false | false | false | let blake2_update_block #al #ms wv hash flag totlen d =
| blake2_compress wv hash d totlen flag | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_key_st | val blake2_update_key_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 115,
"end_line": 942,
"start_col": 0,
"start_line": 932
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_key",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_update_key",
"Lib.Buffer.op_Brack_Lens_Access"
] | [] | false | false | false | true | true | let blake2_update_key_st (al: Spec.alg) (ms: m_spec) =
|
wv: state_p al ms ->
hash: state_p al ms ->
kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al} ->
k: lbuffer uint8 kk ->
ll: size_t
-> Stack unit
(requires
(fun h ->
live h wv /\ live h hash /\ live h k /\ disjoint hash k /\ disjoint wv hash /\
disjoint wv k))
(ensures
(fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[| k |] (v ll) (state_v h0 hash))
) | false |
|
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_multi_st | val blake2_update_multi_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 52,
"end_line": 850,
"start_col": 0,
"start_line": 837
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.op_GreaterThanOrEqual",
"Lib.Buffer.length",
"Lib.Buffer.MUT",
"FStar.Mul.op_Star",
"Hacl.Impl.Blake2.Core.size_block",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Lib.Buffer.live",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Lib.LoopCombinators.repeati",
"Spec.Blake2.blake2_update1",
"Lib.Buffer.op_Brack_Lens_Access"
] | [] | false | false | false | true | true | let blake2_update_multi_st (al: Spec.alg) (ms: m_spec) =
|
#len: size_t ->
wv: state_p al ms ->
hash: state_p al ms ->
prev: Spec.limb_t al {v prev + v len <= Spec.max_limb al} ->
blocks: lbuffer uint8 len ->
nb: size_t{length blocks >= v nb * v (size_block al)}
-> Stack unit
(requires
(fun h ->
live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\
disjoint wv blocks))
(ensures
(fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[| blocks |]) (state_v h0 hash))) | false |
|
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.impl_state_len | val impl_state_len (al: Spec.alg) (ms: m_spec) : size_t | val impl_state_len (al: Spec.alg) (ms: m_spec) : size_t | let impl_state_len (al:Spec.alg) (ms:m_spec) : size_t = //{size_v s == impl_state_length i} =
(**) mul_mod_lemma 4ul (row_len al ms);
match al, ms with
| Spec.Blake2S, M32
| Spec.Blake2B, M32 | Spec.Blake2B, M128 -> 16ul
| Spec.Blake2S, M128 | Spec.Blake2S, M256
| Spec.Blake2B, M256 -> 4ul | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 31,
"end_line": 1073,
"start_col": 0,
"start_line": 1067
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final)
inline_for_extraction noextract
let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms
inline_for_extraction noextract
let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv))
(fun _ h1 -> live h1 hash /\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get() in
if ll =. 0ul then
blake2_update_block wv hash true lb key_block
else
blake2_update_block wv hash false lb key_block)
inline_for_extraction noextract
let blake2_update_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t
-> d: lbuffer uint8 ll ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\ live h d /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k /\
disjoint hash d /\ disjoint wv d /\ disjoint d k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update al (v kk) h0.[|k|] h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_key_st al ms
-> blake2_update_blocks_st al ms
-> blake2_update_st al ms
inline_for_extraction noextract
let blake2_update #al #ms blake2_update_key blake2_update_blocks
wv hash kk k ll d =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
if kk >. 0ul then (
blake2_update_key wv hash kk k ll;
if ll =. 0ul then ()
else blake2_update_blocks wv hash lb d)
else blake2_update_blocks wv hash (size_to_limb al 0ul) d
inline_for_extraction noextract
let blake2_st (al:Spec.alg) (ms:m_spec) =
output: buffer_t MUT uint8
-> output_len: size_t{v output_len == length output /\ 1 <= v output_len /\ v output_len <= Spec.max_output al}
-> input: buffer_t MUT uint8
-> input_len: size_t{v input_len == length input}
-> key: buffer_t MUT uint8
-> key_len: size_t{v key_len == length key /\ v key_len <= Spec.max_key al} ->
Stack unit
(requires (fun h -> live h output /\ live h input /\ live h key
/\ disjoint output input /\ disjoint output key /\ disjoint input key))
(ensures (fun h0 _ h1 -> modifies1 output h0 h1
/\ h1.[|(output <: lbuffer uint8 output_len)|] == Spec.blake2 al h0.[|(input <: lbuffer uint8 input_len)|] (Spec.blake2_default_params al) (v key_len) h0.[|(key <: lbuffer uint8 key_len)|] (v output_len)))
inline_for_extraction noextract
val blake2:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
-> blake2_update_st al ms
-> blake2_finish_st al ms
-> blake2_st al ms
#push-options "--z3rlimit 100"
let blake2 #al #ms blake2_init blake2_update blake2_finish output output_len input input_len key key_len =
[@inline_let]
let stlen = le_sigh al ms in
[@inline_let]
let stzero = zero_element al ms in
let h0 = ST.get() in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2 al h0.[|(input <: lbuffer uint8 input_len)|] (Spec.blake2_default_params al) (v key_len) h0.[|key|] (v output_len) in
salloc1 h0 stlen stzero (Ghost.hide (loc output)) spec
(fun h ->
assert (max_size_t <= Spec.max_limb al);
let h1 = ST.get() in
salloc1 h1 stlen stzero (Ghost.hide (loc output |+| loc h)) spec
(fun wv ->
blake2_init h key_len output_len;
blake2_update wv h key_len key input_len input;
blake2_finish output_len output h))
#pop-options
module B = LowStar.Buffer
module M = LowStar.Modifies
// A little wrapper needed by EverCrypt.Hash
inline_for_extraction noextract
let blake2_malloc_st (al:Spec.alg) (ms:m_spec) = r:rid -> ST.ST (state_p al ms)
(requires (fun h ->
ST.is_eternal_region r))
(ensures (fun h0 s h1 ->
live h1 s /\
M.(modifies loc_none h0 h1) /\
B.fresh_loc (loc_addr_of_buffer s) h0 h1 /\
(M.loc_includes (M.loc_region_only true r) (loc_addr_of_buffer s)) /\
freeable s))
inline_for_extraction noextract
val blake2_malloc:
al:Spec.alg
-> ms:m_spec
-> blake2_malloc_st al ms
#push-options "--ifuel 1" | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": true,
"full_module": "LowStar.Modifies",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Lib.IntTypes.size_t | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"FStar.Pervasives.Native.Mktuple2",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.size_t",
"Prims.unit",
"Lib.IntTypes.mul_mod_lemma",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Blake2.Core.row_len"
] | [] | false | false | false | true | false | let impl_state_len (al: Spec.alg) (ms: m_spec) : size_t =
| mul_mod_lemma 4ul (row_len al ms);
match al, ms with
| Spec.Blake2S, M32 | Spec.Blake2B, M32 | Spec.Blake2B, M128 -> 16ul
| Spec.Blake2S, M128 | Spec.Blake2S, M256 | Spec.Blake2B, M256 -> 4ul | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_finish_st | val blake2_finish_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 93,
"end_line": 904,
"start_col": 0,
"start_line": 897
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_output",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Hacl.Impl.Blake2.Core.state_p",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.loc",
"Prims.eq2",
"Lib.Sequence.lseq",
"Lib.Buffer.op_Brack_Lens_Access",
"Spec.Blake2.blake2_finish",
"Hacl.Impl.Blake2.Core.state_v"
] | [] | false | false | false | true | true | let blake2_finish_st (al: Spec.alg) (ms: m_spec) =
|
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
output: lbuffer uint8 nn ->
hash: state_p al ms
-> Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures
(fun h0 _ h1 ->
modifies (loc output) h0 h1 /\
h1.[| output |] == Spec.blake2_finish al (state_v h0 hash) (v nn))) | false |
|
CQueue.Cell.fst | CQueue.Cell.intro_ccell_is_lvalue | val intro_ccell_is_lvalue
(#opened: _)
(#a: Type)
(c: ccell_ptrvalue a)
: SteelGhost unit opened
emp
(fun _ -> ccell_is_lvalue c)
(fun _ -> ccell_ptrvalue_is_null c == false)
(fun _ res h' ->
ccell_ptrvalue_is_null c == false /\
(h' (ccell_is_lvalue c) <: ccell_ptrvalue a) == c
) | val intro_ccell_is_lvalue
(#opened: _)
(#a: Type)
(c: ccell_ptrvalue a)
: SteelGhost unit opened
emp
(fun _ -> ccell_is_lvalue c)
(fun _ -> ccell_ptrvalue_is_null c == false)
(fun _ res h' ->
ccell_ptrvalue_is_null c == false /\
(h' (ccell_is_lvalue c) <: ccell_ptrvalue a) == c
) | let intro_ccell_is_lvalue
#_ #a c
=
intro_vrefine emp (ccell_is_lvalue_refine c);
intro_vrewrite (emp `vrefine` ccell_is_lvalue_refine c) (ccell_is_lvalue_rewrite c);
change_slprop_rel
(ccell_is_lvalue0 c)
(ccell_is_lvalue c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell_is_lvalue c) == hp_of (ccell_is_lvalue0 c));
assert_norm (sel_of (ccell_is_lvalue c) m === sel_of (ccell_is_lvalue0 c) m)
) | {
"file_name": "share/steel/examples/steel/CQueue.Cell.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 5,
"end_line": 71,
"start_col": 0,
"start_line": 59
} | module CQueue.Cell
(* A Steel model of C cell structs *)
#push-options "--__no_positivity"
noeq
type mcell (a: Type0) = {
data: ref a;
next: ref (mcell a);
all_or_none_null: squash (is_null data == is_null next); // TODO: /\ freeable data /\ freeable next, if freeable is implemented as a pure space proposition rather than as stateful permissions (i.e. "freeable if you have the whole permission")
}
#pop-options
let ccell_ptrvalue a = mcell a
let ccell_ptrvalue_null a = {data = null; next = null; all_or_none_null = ()}
let ccell_ptrvalue_is_null #a x = is_null x.data
let ccell_data #a c =
c.data
let ccell_next #a c =
c.next
let ccell_is_lvalue_refine
(#a: Type)
(c: ccell_ptrvalue a)
(_: t_of emp)
: Tot prop
= ccell_ptrvalue_is_null c == false
let ccell_is_lvalue_rewrite
(#a: Type)
(c: ccell_ptrvalue a)
(_: normal (t_of (emp `vrefine` ccell_is_lvalue_refine c)))
: GTot (ccell_lvalue a)
= c
[@@ __steel_reduce__; __reduce__ ]
let ccell_is_lvalue0
(#a: Type)
(c: ccell_ptrvalue a)
: Tot vprop
= emp `vrefine` ccell_is_lvalue_refine c `vrewrite` ccell_is_lvalue_rewrite c
let ccell_is_lvalue_hp
(#a: Type)
(c: ccell_ptrvalue a)
: Tot (slprop u#1)
= hp_of (ccell_is_lvalue0 c)
let ccell_is_lvalue_sel
(#a: Type)
(c: ccell_ptrvalue a)
: GTot (selector (ccell_lvalue a) (ccell_is_lvalue_hp c))
= sel_of (ccell_is_lvalue0 c) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "CQueue.Cell.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "CQueue",
"short_module": null
},
{
"abbrev": false,
"full_module": "CQueue",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | c: CQueue.Cell.ccell_ptrvalue a -> Steel.Effect.Atomic.SteelGhost Prims.unit | Steel.Effect.Atomic.SteelGhost | [] | [] | [
"Steel.Memory.inames",
"CQueue.Cell.ccell_ptrvalue",
"Steel.Effect.Atomic.change_slprop_rel",
"CQueue.Cell.ccell_is_lvalue0",
"CQueue.Cell.ccell_is_lvalue",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Prims.eq2",
"Prims.prop",
"Steel.Memory.mem",
"FStar.Pervasives.assert_norm",
"Prims.op_Equals_Equals_Equals",
"Steel.Effect.Common.sel_of",
"Prims.unit",
"Steel.Memory.slprop",
"Steel.Effect.Common.hp_of",
"Steel.Effect.Atomic.intro_vrewrite",
"Steel.Effect.Common.vrefine",
"Steel.Effect.Common.emp",
"CQueue.Cell.ccell_is_lvalue_refine",
"CQueue.Cell.ccell_lvalue",
"CQueue.Cell.ccell_is_lvalue_rewrite",
"Steel.Effect.Atomic.intro_vrefine"
] | [] | false | true | false | false | false | let intro_ccell_is_lvalue #_ #a c =
| intro_vrefine emp (ccell_is_lvalue_refine c);
intro_vrewrite (emp `vrefine` (ccell_is_lvalue_refine c)) (ccell_is_lvalue_rewrite c);
change_slprop_rel (ccell_is_lvalue0 c)
(ccell_is_lvalue c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell_is_lvalue c) == hp_of (ccell_is_lvalue0 c));
assert_norm (sel_of (ccell_is_lvalue c) m === sel_of (ccell_is_lvalue0 c) m)) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_st | val blake2_update_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_update_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t
-> d: lbuffer uint8 ll ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\ live h d /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k /\
disjoint hash d /\ disjoint wv d /\ disjoint d k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update al (v kk) h0.[|k|] h0.[|d|] (state_v h0 hash))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 113,
"end_line": 979,
"start_col": 0,
"start_line": 967
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final)
inline_for_extraction noextract
let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms
inline_for_extraction noextract
let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv))
(fun _ h1 -> live h1 hash /\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get() in
if ll =. 0ul then
blake2_update_block wv hash true lb key_block
else
blake2_update_block wv hash false lb key_block) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_key",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"Lib.Buffer.live",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_update",
"Lib.Buffer.op_Brack_Lens_Access"
] | [] | false | false | false | true | true | let blake2_update_st (al: Spec.alg) (ms: m_spec) =
|
wv: state_p al ms ->
hash: state_p al ms ->
kk: size_t{v kk <= Spec.max_key al} ->
k: lbuffer uint8 kk ->
ll: size_t ->
d: lbuffer uint8 ll
-> Stack unit
(requires
(fun h ->
live h wv /\ live h hash /\ live h k /\ live h d /\ disjoint hash k /\ disjoint wv hash /\
disjoint wv k /\ disjoint hash d /\ disjoint wv d /\ disjoint d k))
(ensures
(fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == Spec.blake2_update al (v kk) h0.[| k |] h0.[| d |] (state_v h0 hash))
) | false |
|
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.g2 | val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))) | val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))) | let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 94,
"end_line": 129,
"start_col": 0,
"start_line": 122
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
wv: Hacl.Impl.Blake2.Core.state_p al m ->
a: Hacl.Impl.Blake2.Core.index_t ->
b: Hacl.Impl.Blake2.Core.index_t ->
x: Hacl.Impl.Blake2.Core.row_p al m
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.index_t",
"Hacl.Impl.Blake2.Core.row_p",
"Lib.Sequence.eq_intro",
"Spec.Blake2.Definitions.row",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.g2",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Blake2.Core.row_v",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Blake2.Core.add_row",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi"
] | [] | false | true | false | false | false | let g2 #al #m wv a b x =
| let h0 = ST.get () in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get () in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_st | val blake2_st : al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | let blake2_st (al:Spec.alg) (ms:m_spec) =
output: buffer_t MUT uint8
-> output_len: size_t{v output_len == length output /\ 1 <= v output_len /\ v output_len <= Spec.max_output al}
-> input: buffer_t MUT uint8
-> input_len: size_t{v input_len == length input}
-> key: buffer_t MUT uint8
-> key_len: size_t{v key_len == length key /\ v key_len <= Spec.max_key al} ->
Stack unit
(requires (fun h -> live h output /\ live h input /\ live h key
/\ disjoint output input /\ disjoint output key /\ disjoint input key))
(ensures (fun h0 _ h1 -> modifies1 output h0 h1
/\ h1.[|(output <: lbuffer uint8 output_len)|] == Spec.blake2 al h0.[|(input <: lbuffer uint8 input_len)|] (Spec.blake2_default_params al) (v key_len) h0.[|(key <: lbuffer uint8 key_len)|] (v output_len))) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 230,
"end_line": 1013,
"start_col": 0,
"start_line": 1002
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final)
inline_for_extraction noextract
let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms
inline_for_extraction noextract
let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv))
(fun _ h1 -> live h1 hash /\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get() in
if ll =. 0ul then
blake2_update_block wv hash true lb key_block
else
blake2_update_block wv hash false lb key_block)
inline_for_extraction noextract
let blake2_update_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t
-> d: lbuffer uint8 ll ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\ live h d /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k /\
disjoint hash d /\ disjoint wv d /\ disjoint d k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update al (v kk) h0.[|k|] h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_key_st al ms
-> blake2_update_blocks_st al ms
-> blake2_update_st al ms
inline_for_extraction noextract
let blake2_update #al #ms blake2_update_key blake2_update_blocks
wv hash kk k ll d =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
if kk >. 0ul then (
blake2_update_key wv hash kk k ll;
if ll =. 0ul then ()
else blake2_update_blocks wv hash lb d)
else blake2_update_blocks wv hash (size_to_limb al 0ul) d | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | al: Spec.Blake2.Definitions.alg -> ms: Hacl.Impl.Blake2.Core.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.Buffer.buffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.uint8",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Lib.IntTypes.U32",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.PUB",
"Lib.Buffer.length",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_output",
"Spec.Blake2.Definitions.max_key",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"Lib.Buffer.live",
"Lib.Buffer.disjoint",
"Lib.Buffer.modifies1",
"Lib.Sequence.lseq",
"Lib.Buffer.op_Brack_Lens_Access",
"Lib.Buffer.lbuffer",
"Spec.Blake2.blake2",
"Spec.Blake2.Definitions.blake2_default_params"
] | [] | false | false | false | true | true | let blake2_st (al: Spec.alg) (ms: m_spec) =
|
output: buffer_t MUT uint8 ->
output_len:
size_t
{v output_len == length output /\ 1 <= v output_len /\ v output_len <= Spec.max_output al} ->
input: buffer_t MUT uint8 ->
input_len: size_t{v input_len == length input} ->
key: buffer_t MUT uint8 ->
key_len: size_t{v key_len == length key /\ v key_len <= Spec.max_key al}
-> Stack unit
(requires
(fun h ->
live h output /\ live h input /\ live h key /\ disjoint output input /\
disjoint output key /\ disjoint input key))
(ensures
(fun h0 _ h1 ->
modifies1 output h0 h1 /\
h1.[| (output <: lbuffer uint8 output_len) |] ==
Spec.blake2 al
h0.[| (input <: lbuffer uint8 input_len) |]
(Spec.blake2_default_params al)
(v key_len)
h0.[| (key <: lbuffer uint8 key_len) |]
(v output_len))) | false |
|
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.free | val free (#a:Type)
(#v:erased a)
(r:ref a)
: STT unit
(pts_to r full_perm v) (fun _ -> emp) | val free (#a:Type)
(#v:erased a)
(r:ref a)
: STT unit
(pts_to r full_perm v) (fun _ -> emp) | let free (#a:Type)
(#v:erased a)
(r:ref a)
: STT unit
(pts_to r full_perm v)
(fun _ -> emp)
= coerce_steel(fun _ -> R.free r);
return () | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 13,
"end_line": 98,
"start_col": 0,
"start_line": 91
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v
let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r)
let pts_to_not_null #a #opened #p #v r
= extract_fact #opened (pts_to r p v) (r =!= null) (R.pts_to_not_null r p v);
()
let alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= let r = coerce_steel (fun _ -> R.alloc x) in
r
let read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun _ -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v)
= let u = coerce_steel (fun _ -> R.read r) in
return u
let write (#a:Type)
(#v:erased a)
(r:ref a)
(x:a)
: STT unit
(pts_to r full_perm v)
(fun _ -> pts_to r full_perm x)
= coerce_steel (fun _ -> R.write r x);
return () | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.ST.HigherReference.ref a -> Steel.ST.Effect.STT Prims.unit | Steel.ST.Effect.STT | [] | [] | [
"FStar.Ghost.erased",
"Steel.ST.HigherReference.ref",
"Steel.ST.Util.return",
"Prims.unit",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty",
"FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.req",
"Steel.Effect.Common.rm",
"Steel.ST.Coercions.coerce_steel",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"Steel.FractionalPermission.full_perm",
"FStar.Ghost.reveal",
"Steel.Effect.Common.emp",
"Prims.l_True",
"Steel.HigherReference.free",
"Steel.ST.HigherReference.pts_to"
] | [] | false | true | false | false | false | let free (#a: Type) (#v: erased a) (r: ref a) : STT unit (pts_to r full_perm v) (fun _ -> emp) =
| coerce_steel (fun _ -> R.free r);
return () | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_blocks | val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms | val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms | let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 49,
"end_line": 894,
"start_col": 0,
"start_line": 891
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
blake2_update_multi: Hacl.Impl.Blake2.Generic.blake2_update_multi_st al ms ->
blake2_update_last: Hacl.Impl.Blake2.Generic.blake2_update_last_st al ms
-> Hacl.Impl.Blake2.Generic.blake2_update_blocks_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Generic.blake2_update_multi_st",
"Hacl.Impl.Blake2.Generic.blake2_update_last_st",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.unit",
"FStar.Pervasives.Native.tuple2",
"Lib.IntTypes.int_t",
"Spec.Blake2.split",
"Prims.nat",
"Prims.l_and",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"Lib.IntTypes.range",
"Hacl.Impl.Blake2.Generic.split_blocks"
] | [] | false | false | false | false | false | let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
| let nb, rem = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update | val blake2_update:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_key_st al ms
-> blake2_update_blocks_st al ms
-> blake2_update_st al ms | val blake2_update:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_key_st al ms
-> blake2_update_blocks_st al ms
-> blake2_update_st al ms | let blake2_update #al #ms blake2_update_key blake2_update_blocks
wv hash kk k ll d =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
if kk >. 0ul then (
blake2_update_key wv hash kk k ll;
if ll =. 0ul then ()
else blake2_update_blocks wv hash lb d)
else blake2_update_blocks wv hash (size_to_limb al 0ul) d | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 61,
"end_line": 999,
"start_col": 0,
"start_line": 991
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final)
inline_for_extraction noextract
let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms
inline_for_extraction noextract
let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv))
(fun _ h1 -> live h1 hash /\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get() in
if ll =. 0ul then
blake2_update_block wv hash true lb key_block
else
blake2_update_block wv hash false lb key_block)
inline_for_extraction noextract
let blake2_update_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t
-> d: lbuffer uint8 ll ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\ live h d /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k /\
disjoint hash d /\ disjoint wv d /\ disjoint d k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update al (v kk) h0.[|k|] h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_key_st al ms
-> blake2_update_blocks_st al ms
-> blake2_update_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
blake2_update_key: Hacl.Impl.Blake2.Generic.blake2_update_key_st al ms ->
blake2_update_blocks: Hacl.Impl.Blake2.Generic.blake2_update_blocks_st al ms
-> Hacl.Impl.Blake2.Generic.blake2_update_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Generic.blake2_update_key_st",
"Hacl.Impl.Blake2.Generic.blake2_update_blocks_st",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.IntTypes.size_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_key",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Lib.IntTypes.op_Greater_Dot",
"FStar.UInt32.__uint_to_t",
"Lib.IntTypes.op_Equals_Dot",
"Prims.unit",
"Prims.bool",
"Hacl.Impl.Blake2.Generic.size_to_limb",
"Prims._assert",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Spec.Blake2.Definitions.limb_inttype",
"Prims.l_and",
"Prims.op_GreaterThanOrEqual",
"Lib.IntTypes.max_size_t",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.size_block",
"Spec.Blake2.Definitions.limb_t",
"Prims.eq2",
"Spec.Blake2.Definitions.nat_to_limb",
"Hacl.Impl.Blake2.Core.size_block"
] | [] | false | false | false | false | false | let blake2_update #al #ms blake2_update_key blake2_update_blocks wv hash kk k ll d =
| let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
if kk >. 0ul
then
(blake2_update_key wv hash kk k ll;
if ll =. 0ul then () else blake2_update_blocks wv hash lb d)
else blake2_update_blocks wv hash (size_to_limb al 0ul) d | false |
CQueue.Cell.fst | CQueue.Cell.free_cell | val free_cell
(#a: Type0)
(c: ccell_ptrvalue a) // could be ccell_lvalue, but ccell gives the right refinement
: SteelT unit
(ccell c)
(fun _ -> emp) | val free_cell
(#a: Type0)
(c: ccell_ptrvalue a) // could be ccell_lvalue, but ccell gives the right refinement
: SteelT unit
(ccell c)
(fun _ -> emp) | let free_cell
#a c
=
let c = elim_ccell c in
free (ccell_data c);
free (ccell_next c) | {
"file_name": "share/steel/examples/steel/CQueue.Cell.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 21,
"end_line": 209,
"start_col": 0,
"start_line": 204
} | module CQueue.Cell
(* A Steel model of C cell structs *)
#push-options "--__no_positivity"
noeq
type mcell (a: Type0) = {
data: ref a;
next: ref (mcell a);
all_or_none_null: squash (is_null data == is_null next); // TODO: /\ freeable data /\ freeable next, if freeable is implemented as a pure space proposition rather than as stateful permissions (i.e. "freeable if you have the whole permission")
}
#pop-options
let ccell_ptrvalue a = mcell a
let ccell_ptrvalue_null a = {data = null; next = null; all_or_none_null = ()}
let ccell_ptrvalue_is_null #a x = is_null x.data
let ccell_data #a c =
c.data
let ccell_next #a c =
c.next
let ccell_is_lvalue_refine
(#a: Type)
(c: ccell_ptrvalue a)
(_: t_of emp)
: Tot prop
= ccell_ptrvalue_is_null c == false
let ccell_is_lvalue_rewrite
(#a: Type)
(c: ccell_ptrvalue a)
(_: normal (t_of (emp `vrefine` ccell_is_lvalue_refine c)))
: GTot (ccell_lvalue a)
= c
[@@ __steel_reduce__; __reduce__ ]
let ccell_is_lvalue0
(#a: Type)
(c: ccell_ptrvalue a)
: Tot vprop
= emp `vrefine` ccell_is_lvalue_refine c `vrewrite` ccell_is_lvalue_rewrite c
let ccell_is_lvalue_hp
(#a: Type)
(c: ccell_ptrvalue a)
: Tot (slprop u#1)
= hp_of (ccell_is_lvalue0 c)
let ccell_is_lvalue_sel
(#a: Type)
(c: ccell_ptrvalue a)
: GTot (selector (ccell_lvalue a) (ccell_is_lvalue_hp c))
= sel_of (ccell_is_lvalue0 c)
let intro_ccell_is_lvalue
#_ #a c
=
intro_vrefine emp (ccell_is_lvalue_refine c);
intro_vrewrite (emp `vrefine` ccell_is_lvalue_refine c) (ccell_is_lvalue_rewrite c);
change_slprop_rel
(ccell_is_lvalue0 c)
(ccell_is_lvalue c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell_is_lvalue c) == hp_of (ccell_is_lvalue0 c));
assert_norm (sel_of (ccell_is_lvalue c) m === sel_of (ccell_is_lvalue0 c) m)
)
let elim_ccell_is_lvalue
#_ #a c
=
change_slprop_rel
(ccell_is_lvalue c)
(ccell_is_lvalue0 c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell_is_lvalue c) == hp_of (ccell_is_lvalue0 c));
assert_norm (sel_of (ccell_is_lvalue c) m === sel_of (ccell_is_lvalue0 c) m)
);
elim_vrewrite (emp `vrefine` ccell_is_lvalue_refine c) (ccell_is_lvalue_rewrite c);
elim_vrefine emp (ccell_is_lvalue_refine c)
[@@ __steel_reduce__]
let ccell0 (a: Type0) (c: ccell_lvalue a) : Tot vprop =
(vptr (ccell_data c) `star` vptr (ccell_next c))
// unfold
let ccell_rewrite
(#a: Type0)
(c: ccell_ptrvalue a)
(x: dtuple2 (ccell_lvalue a) (vdep_payload (ccell_is_lvalue c) (ccell0 a)))
: GTot (vcell a)
= let p =
dsnd #(ccell_lvalue a) #(vdep_payload (ccell_is_lvalue c) (ccell0 a)) x
in
{
vcell_data = fst p;
vcell_next = snd p;
}
[@@ __steel_reduce__ ; __reduce__] // to avoid manual unfoldings through change_slprop
let ccell1
(#a: Type0)
(c: ccell_ptrvalue a)
: Tot vprop
= ccell_is_lvalue c `vdep` ccell0 a `vrewrite` ccell_rewrite c
let ccell_hp
#a c
= hp_of (ccell1 c)
let ccell_sel
#a c
= sel_of (ccell1 c)
let intro_ccell
#opened #a c
=
intro_ccell_is_lvalue c;
reveal_star (vptr (ccell_data c)) (vptr (ccell_next c));
intro_vdep
(ccell_is_lvalue c)
(vptr (ccell_data c) `star` vptr (ccell_next c))
(ccell0 a);
intro_vrewrite
(ccell_is_lvalue c `vdep` ccell0 a)
(ccell_rewrite c);
change_slprop_rel
(ccell1 c)
(ccell c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell1 c) == ccell_hp c);
assert_norm (sel_of (ccell1 c) m === sel_of (ccell c) m)
)
let elim_ccell_ghost
#opened #a c
=
change_slprop_rel
(ccell c)
(ccell1 c)
(fun x y -> x == y)
(fun m ->
assert_norm (hp_of (ccell1 c) == ccell_hp c);
assert_norm (sel_of (ccell1 c) m === sel_of (ccell c) m)
);
elim_vrewrite
(ccell_is_lvalue c `vdep` ccell0 a)
(ccell_rewrite c);
let c' : Ghost.erased (ccell_lvalue a) = elim_vdep
(ccell_is_lvalue c)
(ccell0 a)
in
elim_ccell_is_lvalue c;
change_equal_slprop
(ccell0 a c')
(vptr (ccell_data (Ghost.reveal c')) `star` vptr (ccell_next (Ghost.reveal c')));
reveal_star (vptr (ccell_data (Ghost.reveal c'))) (vptr (ccell_next (Ghost.reveal c')));
c'
let elim_ccell
#opened #a c
=
let c2 = elim_ccell_ghost c in
let c : ccell_lvalue a = c in
change_equal_slprop (vptr (ccell_data c2)) (vptr (ccell_data c));
change_equal_slprop (vptr (ccell_next c2)) (vptr (ccell_next c));
return c
let ccell_not_null
#opened #a c
=
let c1 = elim_ccell_ghost c in
let c2 : ccell_lvalue a = c in
change_equal_slprop (vptr (ccell_data c1)) (vptr (ccell_data c2));
change_equal_slprop (vptr (ccell_next c1)) (vptr (ccell_next c2));
intro_ccell c2;
change_equal_slprop (ccell c2) (ccell c);
()
let ralloc (#a:Type0) (x:a) : Steel (ref a)
emp (fun r -> vptr r)
(requires fun _ -> True)
(ensures fun _ r h1 -> h1 (vptr r) == x /\ not (is_null r))
=
malloc x
let alloc_cell
#a data next
=
let rdata = ralloc data in
let rnext = ralloc next in
let res : ccell_lvalue a = ({ data = rdata; next = rnext; all_or_none_null = () }) in
change_equal_slprop (vptr rdata) (vptr (ccell_data res));
change_equal_slprop (vptr rnext) (vptr (ccell_next res));
intro_ccell res;
return res | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "CQueue.Cell.fst"
} | [
{
"abbrev": false,
"full_module": "Steel.Reference",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.FractionalPermission",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect.Atomic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "CQueue",
"short_module": null
},
{
"abbrev": false,
"full_module": "CQueue",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | c: CQueue.Cell.ccell_ptrvalue a -> Steel.Effect.SteelT Prims.unit | Steel.Effect.SteelT | [] | [] | [
"CQueue.Cell.ccell_ptrvalue",
"Steel.Reference.free",
"CQueue.Cell.ccell_next",
"Prims.unit",
"CQueue.Cell.ccell_data",
"CQueue.Cell.ccell_lvalue",
"CQueue.Cell.elim_ccell",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty"
] | [] | false | true | false | false | false | let free_cell #a c =
| let c = elim_ccell c in
free (ccell_data c);
free (ccell_next c) | false |
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.write | val write (#a:Type)
(#v:erased a)
(r:ref a)
(x:a)
: STT unit
(pts_to r full_perm v)
(fun _ -> pts_to r full_perm x) | val write (#a:Type)
(#v:erased a)
(r:ref a)
(x:a)
: STT unit
(pts_to r full_perm v)
(fun _ -> pts_to r full_perm x) | let write (#a:Type)
(#v:erased a)
(r:ref a)
(x:a)
: STT unit
(pts_to r full_perm v)
(fun _ -> pts_to r full_perm x)
= coerce_steel (fun _ -> R.write r x);
return () | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 13,
"end_line": 89,
"start_col": 0,
"start_line": 81
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v
let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r)
let pts_to_not_null #a #opened #p #v r
= extract_fact #opened (pts_to r p v) (r =!= null) (R.pts_to_not_null r p v);
()
let alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= let r = coerce_steel (fun _ -> R.alloc x) in
r
let read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun _ -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v)
= let u = coerce_steel (fun _ -> R.read r) in
return u | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.ST.HigherReference.ref a -> x: a -> Steel.ST.Effect.STT Prims.unit | Steel.ST.Effect.STT | [] | [] | [
"FStar.Ghost.erased",
"Steel.ST.HigherReference.ref",
"Steel.ST.Util.return",
"Prims.unit",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty",
"Steel.ST.HigherReference.pts_to",
"Steel.FractionalPermission.full_perm",
"Steel.Effect.Common.vprop",
"Steel.ST.Coercions.coerce_steel",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"FStar.Ghost.reveal",
"Prims.l_True",
"Steel.HigherReference.write"
] | [] | false | true | false | false | false | let write (#a: Type) (#v: erased a) (r: ref a) (x: a)
: STT unit (pts_to r full_perm v) (fun _ -> pts_to r full_perm x) =
| coerce_steel (fun _ -> R.write r x);
return () | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_finish | val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms | val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms | let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 22,
"end_line": 928,
"start_col": 0,
"start_line": 909
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Hacl.Impl.Blake2.Generic.blake2_finish_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_output",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.Buffer.salloc1",
"Prims.unit",
"Lib.IntTypes.u8",
"FStar.Ghost.hide",
"LowStar.Monotonic.Buffer.loc",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Lib.Buffer.copy",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Lib.Buffer.sub",
"Lib.IntTypes.size",
"Lib.Sequence.eq_intro",
"Lib.Buffer.as_seq",
"Lib.Sequence.op_At_Bar",
"Hacl.Impl.Blake2.Core.size_row",
"Lib.Buffer.gsub",
"FStar.UInt32.__uint_to_t",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Blake2.Core.store_row",
"Hacl.Impl.Blake2.Core.element_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi",
"Hacl.Impl.Blake2.Core.row_p",
"Prims.logical",
"Prims.eq2",
"Lib.Sequence.lseq",
"Lib.IntTypes.uint_t",
"Lib.Buffer.op_Brack_Lens_Access",
"Spec.Blake2.blake2_finish",
"Hacl.Impl.Blake2.Core.state_v",
"Lib.IntTypes.op_Star_Dot"
] | [] | false | false | false | false | false | let blake2_finish #al #ms nn output hash =
| let h0 = ST.get () in
[@@ inline_let ]let double_row = 2ul *. size_row al in
[@@ inline_let ]let spec _ h1 = h1.[| output |] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0
double_row
(u8 0)
(Ghost.hide (loc output))
spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get () in
Lib.Sequence.eq_intro (as_seq h1 full)
(let open Lib.Sequence in
as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al)));
let final = sub full (size 0) nn in
copy output final) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_compress3 | val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))) | val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))) | let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 89,
"end_line": 433,
"start_col": 0,
"start_line": 401
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s_iv: Hacl.Impl.Blake2.Core.state_p al ms -> wv: Hacl.Impl.Blake2.Core.state_p al ms
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.Sequence.eq_intro",
"Spec.Blake2.Definitions.row",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_compress3",
"Prims.unit",
"Lib.Sequence.op_String_Assignment",
"Hacl.Impl.Blake2.Core.row_v",
"Prims._assert",
"Prims.eq2",
"Spec.Blake2.Definitions.op_Hat_Bar",
"Lib.Sequence.op_String_Access",
"Lib.Buffer.modifies",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.Impl.Blake2.Core.xor_row",
"Lib.Buffer.disjoint",
"Lib.Buffer.lbuffer_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Blake2.Core.row_p"
] | [] | false | true | false | false | false | let blake2_compress3 #al #ms s_iv wv =
| let h0 = ST.get () in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get () in
xor_row s0 r2;
let h2 = ST.get () in
xor_row s1 r1;
let h3 = ST.get () in
xor_row s1 r3;
let h4 = ST.get () in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[ 0 ]);
assert (row_v h1 r2 == (state_v h0 wv).[ 2 ]);
assert (row_v h4 s0 ==
Spec.(((state_v h0 s_iv).[ 0 ] ^| (state_v h0 wv).[ 0 ]) ^| (state_v h0 wv).[ 2 ]));
assert (row_v h4 s1 ==
Spec.(((state_v h0 s_iv).[ 1 ] ^| (state_v h0 wv).[ 1 ]) ^| (state_v h0 wv).[ 3 ]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[ 0 ] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[ 1 ] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.unconstrained_bounded_integer | val unconstrained_bounded_integer (sz: integer_size) (i: bounded_integer sz) : GTot bool | val unconstrained_bounded_integer (sz: integer_size) (i: bounded_integer sz) : GTot bool | let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 6,
"end_line": 186,
"start_col": 0,
"start_line": 182
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sz: LowParse.Spec.BoundedInt.integer_size -> i: LowParse.Spec.BoundedInt.bounded_integer sz
-> Prims.GTot Prims.bool | Prims.GTot | [
"sometrivial"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool"
] | [] | false | false | false | false | false | let unconstrained_bounded_integer (sz: integer_size) (i: bounded_integer sz) : GTot bool =
| true | false |
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.pts_to_injective_eq | val pts_to_injective_eq (#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1: a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1) | val pts_to_injective_eq (#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1: a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1) | let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r) | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 90,
"end_line": 54,
"start_col": 0,
"start_line": 42
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.ST.HigherReference.ref a -> Steel.ST.Effect.Ghost.STGhost Prims.unit | Steel.ST.Effect.Ghost.STGhost | [] | [] | [
"Steel.Memory.inames",
"Steel.FractionalPermission.perm",
"Steel.ST.HigherReference.ref",
"Steel.ST.Coercions.coerce_ghost",
"Prims.unit",
"Steel.Effect.Common.star",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"FStar.Ghost.reveal",
"FStar.Ghost.hide",
"Steel.Effect.Common.vprop",
"Prims.l_True",
"Prims.eq2",
"FStar.Ghost.erased",
"Steel.HigherReference.higher_ref_pts_to_injective_eq",
"Steel.ST.HigherReference.pts_to"
] | [] | false | true | false | false | false | let pts_to_injective_eq (#a: Type) (#opened: inames) (#p0 #p1: perm) (#v0 #v1: a) (r: ref a)
: STGhost unit
opened
((pts_to r p0 v0) `star` (pts_to r p1 v1))
(fun _ -> (pts_to r p0 v0) `star` (pts_to r p1 v0))
(requires True)
(ensures fun _ -> v0 == v1) =
| coerce_ghost (fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_multi | val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms | val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms | let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 74,
"end_line": 870,
"start_col": 0,
"start_line": 857
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | blake2_update_block: Hacl.Impl.Blake2.Generic.blake2_update_block_st al ms
-> Hacl.Impl.Blake2.Generic.blake2_update_multi_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Generic.blake2_update_block_st",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.op_GreaterThanOrEqual",
"Lib.Buffer.length",
"Lib.Buffer.MUT",
"FStar.Mul.op_Star",
"Hacl.Impl.Blake2.Core.size_block",
"Lib.Buffer.loop_refl",
"Prims.op_LessThan",
"Hacl.Impl.Blake2.Generic.blake2_update1",
"Prims.unit",
"Lib.LoopCombinators.unfold_repeati",
"Lib.Sequence.lseq",
"Lib.IntTypes.int_t",
"Spec.Blake2.Definitions.wt",
"Hacl.Impl.Blake2.Core.state_v",
"FStar.Monotonic.HyperStack.mem",
"Prims.nat",
"Prims.l_and",
"Prims.op_Division",
"Lib.Sequence.length",
"Lib.IntTypes.U8",
"Lib.Buffer.op_Brack_Lens_Access",
"Spec.Blake2.Definitions.size_block",
"Lib.IntTypes.U64",
"Lib.IntTypes.U128",
"Lib.IntTypes.inttype",
"Spec.Blake2.blake2_update1",
"FStar.Ghost.erased",
"LowStar.Monotonic.Buffer.loc",
"FStar.Ghost.hide",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Hacl.Impl.Blake2.Core.element_t",
"Spec.Blake2.Definitions.state",
"FStar.HyperStack.ST.get"
] | [] | false | false | false | false | false | let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
| let h0 = ST.get () in
[@@ inline_let ]let a_spec = Spec.state al in
[@@ inline_let ]let refl h = state_v h hash in
[@@ inline_let ]let footprint = Ghost.hide (loc hash |+| loc wv) in
[@@ inline_let ]let spec h = Spec.blake2_update1 al (v prev) h.[| blocks |] in
loop_refl h0
nb
a_spec
refl
footprint
spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_payload_kind | val parse_vldata_payload_kind (sz: integer_size) (k: parser_kind) : parser_kind | val parse_vldata_payload_kind (sz: integer_size) (k: parser_kind) : parser_kind | let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 33,
"start_col": 0,
"start_line": 25
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sz: LowParse.Spec.BoundedInt.integer_size -> k: LowParse.Spec.Base.parser_kind
-> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.strong_parser_kind",
"LowParse.Spec.VLData.parse_vldata_payload_size",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.ParserKindMetadataFail",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.None",
"LowParse.Spec.Base.parser_kind_metadata_t"
] | [] | false | false | false | true | false | let parse_vldata_payload_kind (sz: integer_size) (k: parser_kind) : parser_kind =
| strong_parser_kind 0
(parse_vldata_payload_size sz)
(match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_payload | val parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz {f i == true})
: Tot (parser (parse_vldata_payload_kind sz k) t) | val parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz {f i == true})
: Tot (parser (parse_vldata_payload_kind sz k) t) | let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i)) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 68,
"end_line": 43,
"start_col": 0,
"start_line": 35
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t ->
i: LowParse.Spec.BoundedInt.bounded_integer sz {f i == true}
-> LowParse.Spec.Base.parser (LowParse.Spec.VLData.parse_vldata_payload_kind sz k) t | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"Prims.eq2",
"LowParse.Spec.Base.weaken",
"LowParse.Spec.VLData.parse_vldata_payload_kind",
"LowParse.Spec.FLData.parse_fldata_kind",
"FStar.UInt32.v",
"LowParse.Spec.FLData.parse_fldata"
] | [] | false | false | false | false | false | let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz {f i == true})
: Tot (parser (parse_vldata_payload_kind sz k) t) =
| weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i)) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_gen_kind | val parse_vldata_gen_kind (sz: integer_size) (k: parser_kind) : Tot parser_kind | val parse_vldata_gen_kind (sz: integer_size) (k: parser_kind) : Tot parser_kind | let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 86,
"start_col": 0,
"start_line": 78
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sz: LowParse.Spec.BoundedInt.integer_size -> k: LowParse.Spec.Base.parser_kind
-> LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.strong_parser_kind",
"Prims.op_Addition",
"LowParse.Spec.VLData.parse_vldata_payload_size",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.ParserKindMetadataFail",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.None",
"LowParse.Spec.Base.parser_kind_metadata_t"
] | [] | false | false | false | true | false | let parse_vldata_gen_kind (sz: integer_size) (k: parser_kind) : Tot parser_kind =
| strong_parser_kind sz
(sz + parse_vldata_payload_size sz)
(match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None) | false |
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.alloc | val alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r)) | val alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r)) | let alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= let r = coerce_steel (fun _ -> R.alloc x) in
r | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 5,
"end_line": 67,
"start_col": 0,
"start_line": 60
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v
let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r)
let pts_to_not_null #a #opened #p #v r
= extract_fact #opened (pts_to r p v) (r =!= null) (R.pts_to_not_null r p v);
() | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> Steel.ST.Effect.ST (Steel.ST.HigherReference.ref a) | Steel.ST.Effect.ST | [] | [] | [
"Steel.ST.HigherReference.ref",
"Steel.HigherReference.ref",
"Steel.ST.Coercions.coerce_steel",
"Steel.Effect.Common.emp",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"Steel.FractionalPermission.full_perm",
"Steel.Effect.Common.vprop",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Negation",
"Steel.HigherReference.is_null",
"Prims.unit",
"Steel.HigherReference.alloc",
"Steel.ST.HigherReference.pts_to",
"Steel.ST.HigherReference.is_null"
] | [] | false | true | false | false | false | let alloc (#a: Type) (x: a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r)) =
| let r = coerce_steel (fun _ -> R.alloc x) in
r | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_fldata_and_then_cases_injective | val parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma (and_then_cases_injective (parse_vldata_payload sz f p)) | val parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma (and_then_cases_injective (parse_vldata_payload sz f p)) | let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g' | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 29,
"end_line": 73,
"start_col": 0,
"start_line": 47
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 64,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Combinators.and_then_cases_injective (LowParse.Spec.VLData.parse_vldata_payload sz
f
p)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"FStar.Classical.forall_intro_3",
"Prims.eq2",
"LowParse.Bytes.bytes",
"Prims.l_Forall",
"Prims.l_imp",
"LowParse.Spec.Combinators.and_then_cases_injective_precond",
"LowParse.Spec.VLData.parse_vldata_payload",
"Prims.l_or",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.Classical.forall_intro",
"FStar.Classical.move_requires",
"Prims._assert",
"LowParse.Spec.Base.injective_postcond",
"FStar.Seq.Base.slice",
"LowParse.Bytes.byte",
"FStar.UInt32.v",
"LowParse.Spec.Base.injective_precond",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.Combinators.and_then_cases_injective"
] | [] | false | false | true | false | false | let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma (and_then_cases_injective (parse_vldata_payload sz f p)) =
| parser_kind_prop_equiv k p;
let g (len1 len2: (len: bounded_integer sz {f len == true})) (b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2)) =
assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g' (len1 len2: (len: bounded_integer sz {f len == true})) (b1: bytes)
: Lemma
(forall (b2: bytes).
and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==>
len1 == len2) =
Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g' | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update_key | val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms | val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms | let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv))
(fun _ h1 -> live h1 hash /\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get() in
if ll =. 0ul then
blake2_update_block wv hash true lb key_block
else
blake2_update_block wv hash false lb key_block) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 56,
"end_line": 964,
"start_col": 0,
"start_line": 952
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms
let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b
inline_for_extraction noextract
let blake2_update_last_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> rem: size_t {v rem <= v len /\ v rem <= Spec.size_block al}
-> d: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_last:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block: blake2_update_block_st al ms
-> blake2_update_last_st al ms
let blake2_update_last #al #ms blake2_update_block #len wv hash prev rem d =
let h0 = ST.get () in
[@inline_let]
let spec _ h1 = state_v h1 hash == Spec.blake2_update_last al (v prev) (v rem) h0.[|d|] (state_v h0 hash) in
salloc1 h0 (size_block al) (u8 0) (Ghost.hide (loc hash |+| loc wv)) spec
(fun last_block ->
let last = sub d (len -! rem) rem in
let h1 = ST.get() in
update_sub last_block 0ul rem last;
let h2 = ST.get() in
as_seq_gsub h1 d (len -! rem) rem;
assert (as_seq h1 last == Seq.sub (as_seq h1 d) (v len - v rem) (v rem));
assert (as_seq h1 last == Seq.slice (as_seq h0 d) (v len - v rem) (v len));
assert (as_seq h2 last_block == Spec.get_last_padded_block al (as_seq h0 d) (v rem));
let totlen = prev +. (size_to_limb al len) in
blake2_update_block wv hash true totlen last_block;
let h3 = ST.get() in
assert (v totlen == v prev + v len);
assert (state_v h3 hash == Spec.blake2_update_block al true (v totlen) (as_seq h2 last_block) (state_v h0 hash)))
inline_for_extraction noextract
let blake2_init_st (al:Spec.alg) (ms:m_spec) =
hash: state_p al ms
-> kk: size_t{v kk <= Spec.max_key al}
-> nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al} ->
Stack unit
(requires (fun h -> live h hash))
(ensures (fun h0 _ h1 -> modifies (loc hash) h0 h1 /\
state_v h1 hash == Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn)))
inline_for_extraction noextract
val serialize_params (al:Spec.alg)
(kk:size_t{v kk <= Spec.max_key al})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al})
(p: blake2_params al)
(b: lbuffer (word_t al) 8ul)
: Stack unit
(requires fun h ->
live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (Spec.nat_to_word al 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
#push-options "--z3rlimit 100 --fuel 0"
inline_for_extraction noextract
let serialize_params_blake2s
(kk:size_t{v kk <= Spec.max_key Spec.Blake2S})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2S})
(p: blake2_params Spec.Blake2S)
(b: lbuffer (word_t Spec.Blake2S) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u32 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u32 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u32 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u32 p.depth) (size 24) in
[@inline_let]
let v0 = (to_u32 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 in
[@inline_let]
let v1 = p.leaf_length in
[@inline_let]
let v2 = p.node_offset in
[@inline_let]
let node_depth_shift_16 = shift_left (to_u32 p.node_depth) (size 16) in
[@inline_let]
let inner_length_shift_16 = shift_left (to_u32 p.inner_length) (size 24) in
[@inline_let]
let v3 = (to_u32 p.xof_length) ^. node_depth_shift_16 ^. inner_length_shift_16 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
// AF: Putting these writes *after* modifications on a subbuffer of b helps with modifies-reasoning:
// By putting them before, F* struggles with proving that b[0..3] is not modified by uints_from_bytes_le
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- v3;
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2s_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u32 (v nn)) ^.
(u32 (v kk) <<. (size 8)) ^.
(u32 (v p.fanout) <<. (size 16)) ^.
(u32 (v p.depth) <<. (size 24)) in
let s1 = p.leaf_length in
let s2 = p.node_offset in
let s3 = (u32 (v p.xof_length)) ^.
(u32 (v p.node_depth) <<. (size 16)) ^.
(u32 (v p.inner_length) <<. (size 24)) in
let salt_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u32.[0] in
let s5 = salt_u32.[1] in
let personal_u32: lseq uint32 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u32.[0] in
let s7 = personal_u32.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
inline_for_extraction noextract
let serialize_params_blake2b
(kk:size_t{v kk <= Spec.max_key Spec.Blake2B})
(nn: size_t{1 <= v nn /\ v nn <= Spec.max_output Spec.Blake2B})
(p: blake2_params Spec.Blake2B)
(b: lbuffer (word_t Spec.Blake2B) 8ul)
: Stack unit
(requires fun h -> live h b /\
blake2_params_inv #Spec.Blake2B h p /\
LowStar.Buffer.loc_disjoint (loc b) (blake2_params_loc p) /\
as_seq h b == Seq.create 8 (u64 0)
)
(ensures fun h0 _ h1 ->
modifies (loc b) h0 h1 /\
as_seq h1 b == Spec.serialize_blake2_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk)))
= let h0 = ST.get () in
[@inline_let]
let kk_shift_8 = shift_left (to_u64 kk) (size 8) in
[@inline_let]
let fanout_shift_16 = shift_left (to_u64 p.fanout) (size 16) in
[@inline_let]
let depth_shift_24 = shift_left (to_u64 p.depth) (size 24) in
[@inline_let]
let leaf_length_shift_32 = shift_left (to_u64 p.leaf_length) (size 32) in
[@inline_let]
let v0 = (to_u64 nn) ^. kk_shift_8 ^. fanout_shift_16 ^. depth_shift_24 ^. leaf_length_shift_32 in
[@inline_let]
let xof_length_shift_32 = shift_left (to_u64 p.xof_length) (size 32) in
[@inline_let]
let v1 = (to_u64 p.node_offset) ^. xof_length_shift_32 in
[@inline_let]
let inner_length_shift_8 = shift_left (to_u64 p.inner_length) (size 8) in
[@inline_let]
let v2 = (to_u64 p.node_depth) ^. inner_length_shift_8 in
uints_from_bytes_le (sub b 4ul 2ul) p.salt;
uints_from_bytes_le (sub b 6ul 2ul) p.personal;
b.(0ul) <- v0;
b.(1ul) <- v1;
b.(2ul) <- v2;
b.(3ul) <- (u64 0);
let h1 = ST.get () in
let aux () : Lemma (as_seq h1 b `Seq.equal` Spec.serialize_blake2b_params
(Spec.set_key_length (Spec.set_digest_length (blake2_params_v h0 p) (v nn)) (v kk))) =
let open Lib.Sequence in
let open Lib.ByteSequence in
let s0 = (u64 (v nn)) ^.
(u64 (v kk) <<. (size 8)) ^.
(u64 (v p.fanout) <<. (size 16)) ^.
(u64 (v p.depth) <<. (size 24)) ^.
(u64 (v p.leaf_length) <<. (size 32)) in
let s1 = (u64 (v p.node_offset)) ^.
(u64 (v p.xof_length) <<. (size 32)) in
// The serialization corresponding to s2 contains node_depth and inner_length,
// followed by the 14 reserved bytes which always seem to be zeros, and can hence
// be ignored when building the corresponding uint64 using xor's
let s2 = (u64 (v p.node_depth)) ^.
(u64 (v p.inner_length) <<. (size 8)) in
// s3 corresponds to the remaining of the reserved bytes
let s3 = u64 0 in
let salt_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_salt p)) in
let s4 = salt_u64.[0] in
let s5 = salt_u64.[1] in
let personal_u64: lseq uint64 2 = uints_from_bytes_le (as_seq h0 (get_personal p)) in
let s6 = personal_u64.[0] in
let s7 = personal_u64.[1] in
[@inline_let]
let l = [s0; s1; s2; s3; s4; s5; s6; s7] in
assert_norm (List.Tot.length l == 8);
// There seems to be something not triggering with createL, requiring the
// following lemma calls, and assert_norms to relate List.index to the
// actual elements
assert_norm (List.Tot.index l 0 == s0);
assert_norm (List.Tot.index l 1 == s1);
assert_norm (List.Tot.index l 2 == s2);
assert_norm (List.Tot.index l 3 == s3);
assert_norm (List.Tot.index l 4 == s4);
assert_norm (List.Tot.index l 5 == s5);
assert_norm (List.Tot.index l 6 == s6);
assert_norm (List.Tot.index l 7 == s7);
of_list_index l 0;
of_list_index l 1;
of_list_index l 2;
of_list_index l 3;
of_list_index l 4;
of_list_index l 5;
of_list_index l 6;
of_list_index l 7
in
aux()
#pop-options
let serialize_params al kk nn p b =
match al with
| Spec.Blake2S -> serialize_params_blake2s kk nn p b
| Spec.Blake2B -> serialize_params_blake2b kk nn p b
inline_for_extraction noextract
val blake2_init:
#al:Spec.alg
-> #ms:m_spec
-> blake2_init_st al ms
let blake2_init #al #ms hash kk nn =
push_frame ();
let h0 = ST.get() in
let tmp = create 8ul (Spec.nat_to_word al 0) in
let r0 = rowi hash 0ul in
let r1 = rowi hash 1ul in
let r2 = rowi hash 2ul in
let r3 = rowi hash 3ul in
let iv0 = get_iv al 0ul in
let iv1 = get_iv al 1ul in
let iv2 = get_iv al 2ul in
let iv3 = get_iv al 3ul in
let iv4 = get_iv al 4ul in
let iv5 = get_iv al 5ul in
let iv6 = get_iv al 6ul in
let iv7 = get_iv al 7ul in
create_row #al #ms r2 iv0 iv1 iv2 iv3;
create_row #al #ms r3 iv4 iv5 iv6 iv7;
let salt = create (salt_len al) (u8 0) in
let personal = create (personal_len al) (u8 0) in
let p = create_default_params al salt personal in
serialize_params al kk nn p tmp;
let tmp0 = tmp.(0ul) in
let tmp1 = tmp.(1ul) in
let tmp2 = tmp.(2ul) in
let tmp3 = tmp.(3ul) in
let tmp4 = tmp.(4ul) in
let tmp5 = tmp.(5ul) in
let tmp6 = tmp.(6ul) in
let tmp7 = tmp.(7ul) in
let iv0' = iv0 ^. tmp0 in
let iv1' = iv1 ^. tmp1 in
let iv2' = iv2 ^. tmp2 in
let iv3' = iv3 ^. tmp3 in
let iv4' = iv4 ^. tmp4 in
let iv5' = iv5 ^. tmp5 in
let iv6' = iv6 ^. tmp6 in
let iv7' = iv7 ^. tmp7 in
create_row #al #ms r0 iv0' iv1' iv2' iv3';
create_row #al #ms r1 iv4' iv5' iv6' iv7';
let h1 = ST.get() in
assert (disjoint hash tmp);
assert (modifies (loc hash `union` loc tmp) h0 h1);
Lib.Sequence.eq_intro (state_v h1 hash) (Spec.blake2_init_hash al (Spec.blake2_default_params al) (v kk) (v nn));
pop_frame ()
#push-options "--z3rlimit 100 --max_fuel 0 --max_ifuel 0"
let _ : squash (inversion Spec.alg) = allow_inversion Spec.alg
inline_for_extraction noextract
val split_blocks: al:Spec.alg -> len:size_t -> r:(size_t & size_t){
let (x,y) = r in
let (sx,sy) = Spec.split al (v len) in
sx == v x /\
sy == v y}
let split_blocks al len =
let nb = len /. size_block al in
let rem = len %. size_block al in
(if rem =. 0ul && nb >. 0ul then nb -! 1ul else nb),
(if rem =. 0ul && nb >. 0ul then size_block al else rem)
inline_for_extraction noextract
let blake2_update_multi_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len
-> nb : size_t{length blocks >= v nb * v (size_block al) } ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\
disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 ->
modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash == repeati (v nb) (Spec.blake2_update1 al (v prev) h0.[|blocks|])
(state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_multi (#al : Spec.alg) (#ms : m_spec) :
blake2_update_block:blake2_update_block_st al ms
-> blake2_update_multi_st al ms
let blake2_update_multi #al #ms blake2_update_block #len wv hash prev blocks nb =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h hash in
[@inline_let]
let footprint = Ghost.hide(loc hash |+| loc wv) in
[@inline_let]
let spec h = Spec.blake2_update1 al (v prev) h.[|blocks|] in
loop_refl h0 nb a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (v nb) (spec h0) (state_v h0 hash) (v i);
blake2_update1 #al #ms blake2_update_block #len wv hash prev blocks i)
inline_for_extraction noextract
let blake2_update_blocks_st (al : Spec.alg) (ms : m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> blocks: lbuffer uint8 len ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h blocks /\ disjoint hash blocks /\ disjoint wv hash /\ disjoint wv blocks))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1 /\
state_v h1 hash ==
Spec.blake2_update_blocks al (v prev) h0.[|blocks|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_blocks (#al : Spec.alg) (#ms : m_spec) :
blake2_update_multi_st al ms
-> blake2_update_last_st al ms
-> blake2_update_blocks_st al ms
let blake2_update_blocks #al #ms blake2_update_multi blake2_update_last #len wv hash prev blocks =
let (nb,rem) = split_blocks al len in
blake2_update_multi wv hash prev blocks nb;
blake2_update_last #len wv hash prev rem blocks
inline_for_extraction noextract
let blake2_finish_st (al:Spec.alg) (ms:m_spec) =
nn: size_t{1 <= v nn /\ v nn <= Spec.max_output al}
-> output: lbuffer uint8 nn
-> hash: state_p al ms ->
Stack unit
(requires (fun h -> live h hash /\ live h output /\ disjoint output hash))
(ensures (fun h0 _ h1 -> modifies (loc output) h0 h1
/\ h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn)))
inline_for_extraction noextract
val blake2_finish:#al:Spec.alg -> #ms:m_spec -> blake2_finish_st al ms
let blake2_finish #al #ms nn output hash =
let h0 = ST.get () in
[@inline_let]
let double_row = 2ul *. size_row al in
[@inline_let]
let spec _ h1 = h1.[|output|] == Spec.blake2_finish al (state_v h0 hash) (v nn) in
salloc1 h0 double_row (u8 0) (Ghost.hide (loc output)) spec
(fun full ->
let first = sub full 0ul (size_row al) in
let second = sub full (size_row al) (size_row al) in
let row0 = rowi hash 0ul in
let row1 = rowi hash 1ul in
store_row first row0;
store_row second row1;
let h1 = ST.get() in
Lib.Sequence.eq_intro (as_seq h1 full)
(Lib.Sequence.(as_seq h1 (gsub full 0ul (size_row al)) @|
as_seq h1 (gsub full (size_row al) (size_row al))));
let final = sub full (size 0) nn in
copy output final)
inline_for_extraction noextract
let blake2_update_key_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> kk: size_t{v kk > 0 /\ v kk <= Spec.max_key al}
-> k: lbuffer uint8 kk
-> ll: size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h k /\
disjoint hash k /\ disjoint wv hash /\ disjoint wv k))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[|k|] (v ll) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_key:
#al:Spec.alg
-> #ms:m_spec
-> blake2_update_block_st al ms
-> blake2_update_key_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | blake2_update_block: Hacl.Impl.Blake2.Generic.blake2_update_block_st al ms
-> Hacl.Impl.Blake2.Generic.blake2_update_key_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Generic.blake2_update_block_st",
"Hacl.Impl.Blake2.Core.state_p",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_key",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Lib.Buffer.salloc1",
"Prims.unit",
"Hacl.Impl.Blake2.Core.size_block",
"Lib.IntTypes.u8",
"FStar.Ghost.hide",
"LowStar.Monotonic.Buffer.loc",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"FStar.Monotonic.HyperStack.mem",
"Lib.Buffer.live",
"Prims.eq2",
"Spec.Blake2.Definitions.state",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_update_key",
"Lib.Buffer.op_Brack_Lens_Access",
"Lib.IntTypes.op_Equals_Dot",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"FStar.HyperStack.ST.get",
"Lib.Buffer.update_sub",
"Prims._assert",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Spec.Blake2.Definitions.limb_inttype",
"Prims.op_GreaterThanOrEqual",
"Lib.IntTypes.max_size_t",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.size_block",
"Spec.Blake2.Definitions.limb_t",
"Spec.Blake2.Definitions.nat_to_limb",
"Hacl.Impl.Blake2.Generic.size_to_limb"
] | [] | false | false | false | false | false | let blake2_update_key #al #ms blake2_update_block wv hash kk k ll =
| let lb = size_to_limb al (size_block al) in
assert (v lb = Spec.size_block al);
let h0 = ST.get () in
salloc1 h0
(size_block al)
(u8 0)
(Ghost.hide (loc hash |+| loc wv))
(fun _ h1 ->
live h1 hash /\
state_v h1 hash == Spec.blake2_update_key al (v kk) h0.[| k |] (v ll) (state_v h0 hash))
(fun key_block ->
update_sub key_block 0ul kk k;
let h1 = ST.get () in
if ll =. 0ul
then blake2_update_block wv hash true lb key_block
else blake2_update_block wv hash false lb key_block) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_compress1 | val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)) | val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)) | let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 96,
"end_line": 360,
"start_col": 0,
"start_line": 332
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
wv: Hacl.Impl.Blake2.Core.state_p al m ->
s_iv: Hacl.Impl.Blake2.Core.state_p al m ->
offset: Spec.Blake2.Definitions.limb_t al ->
flag: Prims.bool
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.bool",
"Lib.Sequence.eq_intro",
"Spec.Blake2.Definitions.row",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_compress1",
"Prims.unit",
"Prims._assert",
"Lib.Buffer.modifies",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.pop_frame",
"Hacl.Impl.Blake2.Core.xor_row",
"Lib.Buffer.lbuffer_t",
"Hacl.Impl.Blake2.Core.row_len",
"Hacl.Impl.Blake2.Core.rowi",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Blake2.Core.row_p",
"Hacl.Impl.Blake2.Core.copy_state",
"Hacl.Impl.Blake2.Core.create_row",
"Lib.IntTypes.int_t",
"Spec.Blake2.Definitions.wt",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.zero",
"Hacl.Impl.Blake2.Core.word_t",
"Lib.IntTypes.ones",
"Lib.IntTypes.inttype",
"FStar.Pervasives.normalize_term",
"FStar.Pervasives.normalize_term_spec",
"Prims.b2t",
"Lib.IntTypes.unsigned",
"Spec.Blake2.Definitions.limb_to_word",
"Lib.IntTypes.op_Greater_Greater_Dot",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"Hacl.Impl.Blake2.Core.alloc_row",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let blake2_compress1 #al #m wv s_iv offset flag =
| let h0 = ST.get () in
push_frame ();
let mask = alloc_row al m in
[@@ inline_let ]let wv_12 = Spec.limb_to_word al offset in
[@@ inline_let ]let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
normalize_term_spec (Spec.wt al);
[@@ inline_let ]let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame ();
let h1 = ST.get () in
assert (modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_compress | val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms | val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms | let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame() | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 13,
"end_line": 460,
"start_col": 0,
"start_line": 453
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Hacl.Impl.Blake2.Generic.compress_t al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.block_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.bool",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Hacl.Impl.Blake2.Generic.blake2_compress3",
"Hacl.Impl.Blake2.Generic.blake2_compress2",
"Hacl.Impl.Blake2.Generic.blake2_compress1",
"Hacl.Impl.Blake2.Generic.blake2_compress0",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.word_t",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.create",
"FStar.UInt32.__uint_to_t",
"Spec.Blake2.Definitions.zero",
"Lib.Buffer.lbuffer",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let blake2_compress #al #ms wv s m offset flag =
| push_frame ();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame () | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_gen_kind_correct | val parse_vldata_gen_kind_correct (sz: integer_size) (k: parser_kind)
: Lemma
((parse_vldata_gen_kind sz k) ==
(and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz))
(parse_vldata_payload_kind sz k))) | val parse_vldata_gen_kind_correct (sz: integer_size) (k: parser_kind)
: Lemma
((parse_vldata_gen_kind sz k) ==
(and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz))
(parse_vldata_payload_kind sz k))) | let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 24,
"end_line": 95,
"start_col": 0,
"start_line": 88
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sz: LowParse.Spec.BoundedInt.integer_size -> k: LowParse.Spec.Base.parser_kind
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.VLData.parse_vldata_gen_kind sz k ==
LowParse.Spec.Combinators.and_then_kind (LowParse.Spec.Combinators.parse_filter_kind (LowParse.Spec.BoundedInt.parse_bounded_integer_kind
sz))
(LowParse.Spec.VLData.parse_vldata_payload_kind sz k)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.Base.parser_kind",
"FStar.Pervasives.assert_norm",
"Prims.eq2",
"LowParse.Spec.Combinators.and_then_kind",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.VLData.parse_vldata_payload_kind",
"LowParse.Spec.VLData.parse_vldata_gen_kind",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_vldata_gen_kind_correct (sz: integer_size) (k: parser_kind)
: Lemma
((parse_vldata_gen_kind sz k) ==
(and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz))
(parse_vldata_payload_kind sz k))) =
| let kl = parse_vldata_gen_kind sz k in
let kr =
and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)
in
assert_norm (kl == kr) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_strong_pred | val parse_bounded_vldata_strong_pred
(min max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0 | val parse_bounded_vldata_strong_pred
(min max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0 | let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 406,
"start_col": 0,
"start_line": 396
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | min: Prims.nat -> max: Prims.nat -> s: LowParse.Spec.Base.serializer p -> x: t -> Prims.GTot Type0 | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte"
] | [] | false | false | false | false | true | let parse_bounded_vldata_strong_pred
(min max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0 =
| let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_update1 | val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms | val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms | let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
let totlen = prev +. size_to_limb al ((i+!1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get() in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 44,
"end_line": 502,
"start_col": 0,
"start_line": 496
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y)))
let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y))
#pop-options
inline_for_extraction noextract
val diag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.diag (state_v h0 wv)))
let diag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 1ul;
permr_row r2 2ul;
permr_row r3 3ul
inline_for_extraction noextract
val undiag: #a:Spec.alg -> #m:m_spec -> wv:state_p a m
-> Stack unit
(requires (fun h -> live h wv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1 /\
state_v h1 wv == Spec.undiag (state_v h0 wv)))
let undiag #a #m wv =
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
let h0 = ST.get() in
permr_row r1 3ul;
permr_row r2 2ul;
permr_row r3 1ul
inline_for_extraction noextract
val gather_state: #a:Spec.alg -> #ms:m_spec -> st:state_p a ms -> m:block_w a -> start:size_t{v start <= 144} -> Stack unit
(requires (fun h -> live h st /\ live h m /\ disjoint st m))
(ensures (fun h0 _ h1 -> modifies (loc st) h0 h1 /\
state_v h1 st == Spec.gather_state a (as_seq h0 m) (v start)))
inline_for_extraction noextract
let get_sigma' (start: size_t { v start <= 144 }) (i: size_t { normalize (i <=. 15ul) }):
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 ->
h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v start + v i])))
=
get_sigma (start +! i)
#push-options "--z3rlimit 500"
let gather_state #a #ms st m start =
let h0 = ST.get() in
let r0 = rowi st 0ul in
let r1 = rowi st 1ul in
let r2 = rowi st 2ul in
let r3 = rowi st 3ul in
let s0 = get_sigma' start 0ul in
let s1 = get_sigma' start 1ul in
let s2 = get_sigma' start 2ul in
let s3 = get_sigma' start 3ul in
let s4 = get_sigma' start 4ul in
let s5 = get_sigma' start 5ul in
let s6 = get_sigma' start 6ul in
let s7 = get_sigma' start 7ul in
let s8 = get_sigma' start 8ul in
let s9 = get_sigma' start 9ul in
let s10 = get_sigma' start 10ul in
let s11 = get_sigma' start 11ul in
let s12 = get_sigma' start 12ul in
let s13 = get_sigma' start 13ul in
let s14 = get_sigma' start 14ul in
let s15 = get_sigma' start 15ul in
let h1 = ST.get() in
gather_row r0 m s0 s2 s4 s6;
let h2 = ST.get() in
gather_row r1 m s1 s3 s5 s7;
let h3 = ST.get() in
gather_row r2 m s8 s10 s12 s14;
let h4 = ST.get() in
gather_row r3 m s9 s11 s13 s15;
let h5 = ST.get() in
assert(modifies (loc st) h0 h5);
Lib.Sequence.eq_intro (state_v h5 st) (Spec.gather_state a (as_seq h0 m) (v start))
inline_for_extraction noextract
val blake2_round : #al:Spec.alg -> #ms:m_spec -> wv:state_p al ms -> m:block_w al -> i:size_t ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_round al (as_seq h0 m) (v i) (state_v h0 wv)))
let blake2_round #al #ms wv m i =
push_frame();
let start_idx = (i %. size 10) *. size 16 in
assert (v start_idx == (v i % 10) * 16);
assert (v start_idx <= 144);
let m_st = alloc_state al ms in
gather_state m_st m start_idx;
let x = rowi m_st 0ul in
let y = rowi m_st 1ul in
let z = rowi m_st 2ul in
let w = rowi m_st 3ul in
let h1 = ST.get() in
assert (disjoint wv m_st);
assert (disjoint m_st wv);
assert (disjoint x wv);
assert (disjoint wv x);
assert (disjoint y wv);
assert (disjoint wv y);
assert (disjoint z wv);
assert (disjoint wv z);
assert (disjoint w wv);
assert (disjoint wv w);
blake2_mixing wv x y;
diag wv;
blake2_mixing wv z w;
undiag wv;
pop_frame ()
inline_for_extraction noextract
val blake2_compress0:
#al:Spec.alg
-> m_s: block_p al
-> m_w: block_w al
-> Stack unit
(requires (fun h -> live h m_s /\ live h m_w /\ disjoint m_s m_w))
(ensures (fun h0 _ h1 -> modifies (loc m_w) h0 h1
/\ as_seq h1 m_w == Spec.blake2_compress0 al (as_seq h0 m_s)))
let blake2_compress0 #al m_s m_w =
uints_from_bytes_le m_w m_s
inline_for_extraction noextract
val blake2_compress1:
#al:Spec.alg
-> #m:m_spec
-> wv: state_p al m
-> s_iv: state_p al m
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s_iv /\ disjoint wv s_iv))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_compress1 al (state_v h0 s_iv) offset flag))
let blake2_compress1 #al #m wv s_iv offset flag =
let h0 = ST.get() in
push_frame();
let mask = alloc_row al m in
[@inline_let]
let wv_12 = Spec.limb_to_word al offset in
[@inline_let]
let wv_13 = Spec.limb_to_word al (offset >>. (size (bits (Spec.wt al)))) in
// SH: TODO: for some reason, ``ones`` below doesn't get inlined by KaRaMeL,
// causing an extraction problem. The 3 lines below are a hack to fix
// extraction for the time being:
// [> let wv_14 = if flag then (ones (Spec.wt al) SEC) else (Spec.zero al) in
// After investigation, it is because ones is [@(strict_on_arguments [0])],
// and so isn't unfolded if its first argument is not normalized to a constant.
// However, the first argument should always be normalized (I checked the
// output generated by KaRaMeL and the definitions).
(**) normalize_term_spec (Spec.wt al);
[@inline_let] let wt_al = normalize_term (Spec.wt al) in
let wv_14 = if flag then ones wt_al SEC else (Spec.zero al) in
// end of the TODO
let wv_15 = Spec.zero al in
create_row mask wv_12 wv_13 wv_14 wv_15;
copy_state wv s_iv;
let wv3 = rowi wv 3ul in
xor_row wv3 mask;
pop_frame();
let h1 = ST.get() in
assert(modifies (loc wv) h0 h1);
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.blake2_compress1 al (state_v h0 s_iv) offset flag)
inline_for_extraction noextract
val blake2_compress2 :
#al:Spec.alg
-> #ms:m_spec
-> wv: state_p al ms
-> m: block_w al ->
Stack unit
(requires (fun h -> live h wv /\ live h m /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies1 wv h0 h1
/\ state_v h1 wv == Spec.blake2_compress2 al (state_v h0 wv) (as_seq h0 m)))
#push-options "--z3rlimit 400"
let blake2_compress2 #al #ms wv m =
let h0 = ST.get () in
[@inline_let]
let a_spec = Spec.state al in
[@inline_let]
let refl h = state_v h wv in
[@inline_let]
let footprint = Ghost.hide(loc wv) in
[@inline_let]
let spec h = Spec.blake2_round al h.[|m|] in
loop_refl h0 (rounds_t al) a_spec refl footprint spec
(fun i ->
Loops.unfold_repeati (Spec.rounds al) (spec h0) (state_v h0 wv) (v i);
blake2_round wv m i)
#pop-options
inline_for_extraction noextract
val blake2_compress3 :
#al:Spec.alg
-> #ms:m_spec
-> s_iv:state_p al ms
-> wv:state_p al ms ->
Stack unit
(requires (fun h -> live h s_iv /\ live h wv /\ disjoint s_iv wv))
(ensures (fun h0 _ h1 -> modifies (loc s_iv) h0 h1
/\ state_v h1 s_iv == Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv)))
let blake2_compress3 #al #ms s_iv wv =
let h0 = ST.get() in
let s0 = rowi s_iv 0ul in
let s1 = rowi s_iv 1ul in
let r0 = rowi wv 0ul in
let r1 = rowi wv 1ul in
let r2 = rowi wv 2ul in
let r3 = rowi wv 3ul in
assert (disjoint s0 wv);
assert (disjoint wv s0);
assert (disjoint s1 wv);
assert (disjoint wv s1);
assert (disjoint r0 s0);
assert (disjoint r2 s0);
assert (disjoint r1 s1);
assert (disjoint r3 s1);
xor_row s0 r0;
let h1 = ST.get() in
xor_row s0 r2;
let h2 = ST.get() in
xor_row s1 r1;
let h3 = ST.get() in
xor_row s1 r3;
let h4 = ST.get() in
assert (modifies (loc s_iv) h0 h4);
let open Lib.Sequence in
assert (row_v h0 r0 == (state_v h0 wv).[0]);
assert (row_v h1 r2 == (state_v h0 wv).[2]);
assert (row_v h4 s0 == Spec.(((state_v h0 s_iv).[0] ^| (state_v h0 wv).[0]) ^| (state_v h0 wv).[2]));
assert (row_v h4 s1 == Spec.(((state_v h0 s_iv).[1] ^| (state_v h0 wv).[1]) ^| (state_v h0 wv).[3]));
eq_intro (state_v h2 s_iv) ((state_v h0 s_iv).[0] <- row_v h4 s0);
eq_intro (state_v h4 s_iv) ((state_v h2 s_iv).[1] <- row_v h4 s1);
eq_intro (state_v h4 s_iv) (Spec.blake2_compress3 al (state_v h0 wv) (state_v h0 s_iv))
inline_for_extraction noextract
let compress_t (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> s: state_p al ms
-> m: block_p al
-> offset: Spec.limb_t al
-> flag: bool ->
Stack unit
(requires (fun h -> live h wv /\ live h s /\ live h m /\ disjoint s m /\ disjoint wv s /\ disjoint wv m))
(ensures (fun h0 _ h1 -> modifies (loc s |+| loc wv) h0 h1
/\ state_v h1 s == Spec.blake2_compress al (state_v h0 s) h0.[|m|] offset flag))
inline_for_extraction noextract
val blake2_compress: #al:Spec.alg -> #ms:m_spec -> compress_t al ms
let blake2_compress #al #ms wv s m offset flag =
push_frame();
let m_w = create 16ul (Spec.zero al) in
blake2_compress0 #al m m_w;
blake2_compress1 wv s offset flag;
blake2_compress2 wv m_w;
blake2_compress3 s wv;
pop_frame()
inline_for_extraction noextract
let blake2_update_block_st (al:Spec.alg) (ms:m_spec) =
wv:state_p al ms
-> hash: state_p al ms
-> flag: bool
-> totlen: Spec.limb_t al{v totlen <= Spec.max_limb al}
-> d: block_p al ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update_block al flag (v totlen) h0.[|d|] (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update_block: #al:Spec.alg -> #ms:m_spec -> blake2_update_block_st al ms
let blake2_update_block #al #ms wv hash flag totlen d =
blake2_compress wv hash d totlen flag
inline_for_extraction noextract
let blake2_update1_st (al:Spec.alg) (ms:m_spec) =
#len:size_t
-> wv: state_p al ms
-> hash: state_p al ms
-> prev: Spec.limb_t al{v prev + v len <= Spec.max_limb al}
-> d: lbuffer uint8 len
-> i: size_t{v i < length d / Spec.size_block al} ->
Stack unit
(requires (fun h -> live h wv /\ live h hash /\ live h d /\ disjoint hash d /\ disjoint wv hash /\ disjoint wv d))
(ensures (fun h0 _ h1 -> modifies (loc hash |+| loc wv) h0 h1
/\ state_v h1 hash == Spec.blake2_update1 al (v prev) h0.[|d|] (v i) (state_v h0 hash)))
inline_for_extraction noextract
val blake2_update1: #al:Spec.alg -> #ms:m_spec -> blake2_update_block: blake2_update_block_st al ms -> blake2_update1_st al ms | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_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": 500,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | blake2_update_block: Hacl.Impl.Blake2.Generic.blake2_update_block_st al ms
-> Hacl.Impl.Blake2.Generic.blake2_update1_st al ms | Prims.Tot | [
"total"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Generic.blake2_update_block_st",
"Lib.IntTypes.size_t",
"Hacl.Impl.Blake2.Core.state_p",
"Spec.Blake2.Definitions.limb_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Addition",
"Lib.IntTypes.v",
"Spec.Blake2.Definitions.limb_inttype",
"Lib.IntTypes.SEC",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Spec.Blake2.Definitions.max_limb",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"Prims.op_LessThan",
"Prims.op_Division",
"Lib.Buffer.length",
"Lib.Buffer.MUT",
"Spec.Blake2.Definitions.size_block",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Lib.Sequence.seq",
"Prims.l_or",
"Prims.nat",
"FStar.Seq.Base.length",
"Hacl.Impl.Blake2.Core.size_block",
"Lib.Buffer.as_seq",
"Spec.Blake2.get_blocki",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U8",
"Lib.Buffer.sub",
"Lib.IntTypes.op_Star_Dot",
"Prims.int",
"FStar.Mul.op_Star",
"Spec.Blake2.Definitions.wt",
"Lib.IntTypes.U64",
"Lib.IntTypes.U128",
"Lib.IntTypes.op_Plus_Dot",
"Hacl.Impl.Blake2.Generic.size_to_limb",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.op_Plus_Bang",
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | false | false | let blake2_update1 #al #ms blake2_update_block #len wv hash prev d i =
| let totlen = prev +. size_to_limb al ((i +! 1ul) *! size_block al) in
assert (v totlen == v prev + (v i + 1) * Spec.size_block al);
let b = sub d (i *. size_block al) (size_block al) in
let h = ST.get () in
assert (as_seq h b == Spec.get_blocki al (as_seq h d) (v i));
blake2_update_block wv hash false totlen b | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_gen_eq_some_elim | val parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma (requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures
(let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\
(let Some (len, consumed_len) = pbi in
consumed_len == sz /\ f len /\ Seq.length input >= sz + U32.v len /\
(let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)))))) | val parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma (requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures
(let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\
(let Some (len, consumed_len) = pbi in
consumed_len == sz /\ f len /\ Seq.length input >= sz + U32.v len /\
(let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)))))) | let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 180,
"start_col": 0,
"start_line": 157
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t ->
input: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
Some? (LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen sz f p) input))
(ensures
(let pbi =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) input
in
Some? pbi /\
(let _ = pbi in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ len consumed_len) =
_
in
consumed_len == sz /\ f len /\ FStar.Seq.Base.length input >= sz + FStar.UInt32.v len /\
(let input' = FStar.Seq.Base.slice input sz (sz + FStar.UInt32.v len) in
let pp = LowParse.Spec.Base.parse p input' in
Some? pp /\
(let _ = pp in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x consumed_x) =
_
in
consumed_x = FStar.UInt32.v len /\
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen sz f p) input ==
FStar.Pervasives.Native.Some (x, sz + FStar.UInt32.v len))
<:
Prims.logical)))
<:
Prims.logical))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.VLData.parse_vldata_gen_eq",
"Prims.unit",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_vldata_gen",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"Prims.l_or",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Addition",
"FStar.UInt32.v",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Prims.logical",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma (requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures
(let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\
(let Some (len, consumed_len) = pbi in
consumed_len == sz /\ f len /\ Seq.length input >= sz + U32.v len /\
(let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)))))) =
| parse_vldata_gen_eq sz f p input | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_strong_kind | val parse_bounded_vldata_strong_kind (min max l: nat) (k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4))
(ensures (fun _ -> True)) | val parse_bounded_vldata_strong_kind (min max l: nat) (k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4))
(ensures (fun _ -> True)) | let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 250,
"start_col": 0,
"start_line": 226
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | min: Prims.nat -> max: Prims.nat -> l: Prims.nat -> k: LowParse.Spec.Base.parser_kind
-> Prims.Pure LowParse.Spec.Base.parser_kind | Prims.Pure | [] | [] | [
"Prims.nat",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.strong_parser_kind",
"Prims.op_Addition",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_metadata",
"FStar.Pervasives.Native.Some",
"LowParse.Spec.Base.parser_kind_metadata_some",
"LowParse.Spec.Base.ParserKindMetadataFail",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.None",
"LowParse.Spec.Base.parser_kind_metadata_t",
"Prims.int",
"Prims.op_LessThan",
"Prims.bool",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"Prims.op_GreaterThan",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"Prims.l_True"
] | [] | false | false | false | false | false | let parse_bounded_vldata_strong_kind (min max l: nat) (k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4))
(ensures (fun _ -> True)) =
| [@@ inline_let ]let kmin = k.parser_kind_low in
[@@ inline_let ]let min' = if kmin > min then kmin else min in
[@@ inline_let ]let max' =
match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@@ inline_let ]let max' = if max' < min' then min' else max' in
strong_parser_kind (l + min')
(l + max')
(match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_elim' | val parse_bounded_vldata_elim'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | val parse_bounded_vldata_elim'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 81,
"end_line": 281,
"start_col": 0,
"start_line": 252
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
p: LowParse.Spec.Base.parser k t ->
xbytes: LowParse.Bytes.bytes ->
x: t ->
consumed: LowParse.Spec.Base.consumed_length xbytes
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen l
(LowParse.Spec.BoundedInt.in_bounds min max)
p)
xbytes ==
FStar.Pervasives.Native.Some (x, consumed))
(ensures
(let sz = l in
let plen =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) xbytes
in
Some? plen /\
(let _ = plen in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ len consumed_len) =
_
in
consumed_len == sz /\ LowParse.Spec.BoundedInt.in_bounds min max len /\
FStar.UInt32.v len <= FStar.Seq.Base.length xbytes - sz /\
(let input' = FStar.Seq.Base.slice xbytes sz (sz + FStar.UInt32.v len) in
let pp = LowParse.Spec.Base.parse p input' in
Some? pp /\
(let _ = pp in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x' consumed_p) =
_
in
x' == x /\ consumed_p == FStar.UInt32.v len /\
consumed == sz + FStar.UInt32.v len)
<:
Prims.logical)))
<:
Prims.logical))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"Prims.unit",
"LowParse.Spec.VLData.parse_vldata_gen_eq",
"LowParse.Spec.BoundedInt.in_bounds",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_vldata_gen",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.UInt32.v",
"Prims.op_Subtraction",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.op_Addition",
"Prims.logical",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.BoundedInt.integer_size",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_bounded_vldata_elim'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) =
| parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_gen_eq | val parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\
(if f len
then
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then res == Some (x, sz + U32.v len) else res == None
| _ -> res == None
else res == None)) | val parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\
(if f len
then
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then res == Some (x, sz + U32.v len) else res == None
| _ -> res == None
else res == None)) | let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
() | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 155,
"start_col": 0,
"start_line": 123
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t ->
input: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(ensures
(let res = LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen sz f p) input in
(match
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) input
with
| FStar.Pervasives.Native.None #_ -> res == FStar.Pervasives.Native.None
| FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ len consumed_len) ->
consumed_len == sz /\
(match f len with
| true ->
(match FStar.Seq.Base.length input < sz + FStar.UInt32.v len with
| true -> res == FStar.Pervasives.Native.None
| _ ->
let input' = FStar.Seq.Base.slice input sz (sz + FStar.UInt32.v len) in
(match LowParse.Spec.Base.parse p input' with
| FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x consumed_x) ->
(match consumed_x = FStar.UInt32.v len with
| true -> res == FStar.Pervasives.Native.Some (x, sz + FStar.UInt32.v len)
| _ -> res == FStar.Pervasives.Native.None)
<:
Prims.logical
| _ -> res == FStar.Pervasives.Native.None)
<:
Prims.logical)
<:
Prims.logical
| _ -> res == FStar.Pervasives.Native.None))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"Prims.unit",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.Combinators.parse_filter_eq",
"LowParse.Spec.Combinators.and_then_eq",
"LowParse.Spec.Combinators.parse_filter_kind",
"LowParse.Spec.Combinators.parse_filter_refine",
"LowParse.Spec.Combinators.parse_filter",
"LowParse.Spec.VLData.parse_vldata_payload_kind",
"LowParse.Spec.VLData.parse_vldata_payload",
"LowParse.Spec.VLData.parse_vldata_gen_eq_def",
"Prims.l_True",
"Prims.squash",
"LowParse.Spec.Base.parse",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"FStar.Pervasives.Native.None",
"Prims.l_and",
"Prims.nat",
"Prims.l_or",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.op_LessThan",
"Prims.op_Addition",
"FStar.UInt32.v",
"Prims.op_Equality",
"Prims.int",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Prims.logical",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.VLData.parse_vldata_gen",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\
(if f len
then
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then res == Some (x, sz + U32.v len) else res == None
| _ -> res == None
else res == None)) =
| parse_vldata_gen_eq_def sz f p;
and_then_eq #_
#(parse_filter_refine f)
(parse_filter (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p)
input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
() | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_vldata_eq | val parse_vldata_eq (sz: integer_size) (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes)
: Lemma
(parse (parse_vldata sz p) input ==
(match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then Some (x, sz + U32.v len) else None
| _ -> None)) | val parse_vldata_eq (sz: integer_size) (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes)
: Lemma
(parse (parse_vldata sz p) input ==
(match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then Some (x, sz + U32.v len) else None
| _ -> None)) | let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 66,
"end_line": 219,
"start_col": 0,
"start_line": 196
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
p: LowParse.Spec.Base.parser k t ->
input: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata sz p) input ==
(match LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) input with
| FStar.Pervasives.Native.None #_ -> FStar.Pervasives.Native.None
| FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ len _) ->
(match FStar.Seq.Base.length input < sz + FStar.UInt32.v len with
| true -> FStar.Pervasives.Native.None
| _ ->
let input' = FStar.Seq.Base.slice input sz (sz + FStar.UInt32.v len) in
(match LowParse.Spec.Base.parse p input' with
| FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x consumed_x) ->
(match consumed_x = FStar.UInt32.v len with
| true -> FStar.Pervasives.Native.Some (x, sz + FStar.UInt32.v len)
| _ -> FStar.Pervasives.Native.None)
<:
FStar.Pervasives.Native.option (t * LowParse.Spec.Base.consumed_length input)
| _ -> FStar.Pervasives.Native.None)
<:
FStar.Pervasives.Native.option (t * LowParse.Spec.Base.consumed_length input))
<:
FStar.Pervasives.Native.option (t * LowParse.Spec.Base.consumed_length input))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.VLData.parse_vldata_gen_eq",
"LowParse.Spec.VLData.unconstrained_bounded_integer",
"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",
"LowParse.Spec.VLData.parse_vldata",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"FStar.Pervasives.Native.None",
"Prims.op_LessThan",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.op_Addition",
"FStar.UInt32.v",
"Prims.bool",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_LessThanOrEqual",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_vldata_eq (sz: integer_size) (#k: parser_kind) (#t: Type) (p: parser k t) (input: bytes)
: Lemma
(parse (parse_vldata sz p) input ==
(match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len then Some (x, sz + U32.v len) else None
| _ -> None)) =
| parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_elim | val parse_bounded_vldata_elim
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | val parse_bounded_vldata_elim
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 362,
"start_col": 0,
"start_line": 334
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
p: LowParse.Spec.Base.parser k t ->
xbytes: LowParse.Bytes.bytes ->
x: t ->
consumed: LowParse.Spec.Base.consumed_length xbytes
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata' min max l p) xbytes ==
FStar.Pervasives.Native.Some (x, consumed))
(ensures
(let sz = l in
let plen =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) xbytes
in
Some? plen /\
(let _ = plen in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ len consumed_len) =
_
in
consumed_len == sz /\ LowParse.Spec.BoundedInt.in_bounds min max len /\
FStar.UInt32.v len <= FStar.Seq.Base.length xbytes - sz /\
(let input' = FStar.Seq.Base.slice xbytes sz (sz + FStar.UInt32.v len) in
let pp = LowParse.Spec.Base.parse p input' in
Some? pp /\
(let _ = pp in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x' consumed_p) =
_
in
x' == x /\ consumed_p == FStar.UInt32.v len /\
consumed == sz + FStar.UInt32.v len)
<:
Prims.logical)))
<:
Prims.logical))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.VLData.parse_bounded_vldata_elim'",
"Prims.unit",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_bounded_vldata'",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_Some",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.in_bounds",
"FStar.UInt32.v",
"Prims.op_Subtraction",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.op_Addition",
"Prims.logical",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.BoundedInt.integer_size",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let parse_bounded_vldata_elim
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures
(let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) =
| parse_bounded_vldata_elim' min max l p xbytes x consumed | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata | val parse_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t) | val parse_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t) | let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 47,
"end_line": 332,
"start_col": 0,
"start_line": 325
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
p: LowParse.Spec.Base.parser k t
-> LowParse.Spec.Base.parser (LowParse.Spec.VLData.parse_bounded_vldata_strong_kind min
max
(LowParse.Spec.BoundedInt.log256' max)
k)
t | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.VLData.parse_bounded_vldata'",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind"
] | [] | false | false | false | false | false | let parse_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t) =
| parse_bounded_vldata' min max (log256' max) p | false |
Steel.ST.HigherReference.fst | Steel.ST.HigherReference.read | val read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun x -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v) | val read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun x -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v) | let read (#a:Type)
(#p:perm)
(#v:erased a)
(r:ref a)
: ST a
(pts_to r p v)
(fun _ -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v)
= let u = coerce_steel (fun _ -> R.read r) in
return u | {
"file_name": "lib/steel/Steel.ST.HigherReference.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 12,
"end_line": 79,
"start_col": 0,
"start_line": 69
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.ST.HigherReference
open FStar.Ghost
open Steel.ST.Util
open Steel.ST.Coercions
module R = Steel.HigherReference
let ref (a:Type u#1)
: Type0
= R.ref a
let null (#a:Type)
: ref a
= R.null #a
let is_null (#a:Type) (r:ref a)
: b:bool{b <==> r == null}
= R.is_null r
let pts_to (#a:Type)
(r:ref a)
([@@@smt_fallback] p:perm)
([@@@smt_fallback] v:a)
: vprop
= R.pts_to r p v
let pts_to_injective_eq
(#a: Type)
(#opened:inames)
(#p0 #p1:perm)
(#v0 #v1:a)
(r: ref a)
: STGhost unit opened
(pts_to r p0 v0 `star` pts_to r p1 v1)
(fun _ -> pts_to r p0 v0 `star` pts_to r p1 v0)
(requires True)
(ensures fun _ -> v0 == v1)
= coerce_ghost
(fun _ -> R.higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #(hide v0) #(hide v1) r)
let pts_to_not_null #a #opened #p #v r
= extract_fact #opened (pts_to r p v) (r =!= null) (R.pts_to_not_null r p v);
()
let alloc (#a:Type) (x:a)
: ST (ref a)
emp
(fun r -> pts_to r full_perm x)
(requires True)
(ensures fun r -> not (is_null r))
= let r = coerce_steel (fun _ -> R.alloc x) in
r | {
"checked_file": "/",
"dependencies": [
"Steel.ST.Util.fsti.checked",
"Steel.ST.Coercions.fsti.checked",
"Steel.HigherReference.fsti.checked",
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": true,
"source_file": "Steel.ST.HigherReference.fst"
} | [
{
"abbrev": true,
"full_module": "Steel.HigherReference",
"short_module": "R"
},
{
"abbrev": false,
"full_module": "Steel.ST.Coercions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "Steel.ST.Util",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.ST.HigherReference.ref a -> Steel.ST.Effect.ST a | Steel.ST.Effect.ST | [] | [] | [
"Steel.FractionalPermission.perm",
"FStar.Ghost.erased",
"Steel.ST.HigherReference.ref",
"Steel.ST.Util.return",
"FStar.Ghost.hide",
"FStar.Set.set",
"Steel.Memory.iname",
"FStar.Set.empty",
"Steel.ST.HigherReference.pts_to",
"FStar.Ghost.reveal",
"Steel.Effect.Common.vprop",
"Steel.ST.Coercions.coerce_steel",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.HigherReference.pts_to_sl",
"Prims.l_True",
"Prims.eq2",
"Prims.unit",
"Steel.HigherReference.read"
] | [] | false | true | false | false | false | let read (#a: Type) (#p: perm) (#v: erased a) (r: ref a)
: ST a
(pts_to r p v)
(fun _ -> pts_to r p v)
(requires True)
(ensures fun x -> x == Ghost.reveal v) =
| let u = coerce_steel (fun _ -> R.read r) in
return u | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_strong | val parse_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max (log256' max) k)
(parse_bounded_vldata_strong_t min max s)) | val parse_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max (log256' max) k)
(parse_bounded_vldata_strong_t min max s)) | let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 54,
"end_line": 470,
"start_col": 0,
"start_line": 462
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s: LowParse.Spec.Base.serializer p
-> LowParse.Spec.Base.parser (LowParse.Spec.VLData.parse_bounded_vldata_strong_kind min
max
(LowParse.Spec.BoundedInt.log256' max)
k)
(LowParse.Spec.VLData.parse_bounded_vldata_strong_t min max s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.parse_bounded_vldata_strong'",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t"
] | [] | false | false | false | false | false | let parse_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max (log256' max) k)
(parse_bounded_vldata_strong_t min max s)) =
| parse_bounded_vldata_strong' min max (log256' max) s | false |
Z3TestGen.fst | Z3TestGen.prelude | val prelude:string | val prelude:string | let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 306,
"start_col": 0,
"start_line": 10
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.string | Prims.Tot | [
"total"
] | [] | [] | [] | false | false | false | true | false | let prelude:string =
| "\n(set-option :produce-models true)\n(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))\n(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))\n\n; From EverParse3d.ErrorCode.is_range_okay\n(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool\n (and\n (>= size access_size)\n (>= (- size access_size) offset)\n )\n)\n\n(define-fun parse-empty ((x State)) Result\n (mk-result 0 x)\n)\n\n(declare-fun choose (Int) Int)\n(assert (forall ((i Int))\n (and (<= 0 (choose i)) (< (choose i) 256))\n))\n\n(declare-fun branch-trace (Int) Int)\n\n(define-fun parse-false ((x State)) State\n (mk-state -1 (choice-index x) (branch-index x))\n)\n\n(define-fun parse-all-bytes ((x State)) State\n (if (<= (input-size x) 0)\n x\n (mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))\n )\n)\n\n(define-fun parse-all-zeros ((x State)) State\n (if (<= (input-size x) 0)\n x\n (mk-state\n (if\n (forall ((j Int))\n (if (and (<= 0 j) (< j (input-size x)))\n (= (choose (+ (choice-index x) j)) 0)\n true\n )\n )\n 0\n -1\n )\n (+ (choice-index x) (input-size x))\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u8 ((x State)) Result\n (mk-result\n (choose (choice-index x))\n (mk-state\n (let ((new-size (- (input-size x) 1)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 1)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u16-be ((x State)) Result\n (mk-result\n (+ (choose (+ 1 (choice-index x)))\n (* 256\n (choose (+ 0 (choice-index x)))\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 2)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 2)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u16-le ((x State)) Result\n (mk-result\n (+ (choose (+ 0 (choice-index x)))\n (* 256\n (choose (+ 1 (choice-index x)))\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 2)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 2)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u32-be ((x State)) Result\n (mk-result\n (+ (choose (+ 3 (choice-index x)))\n (* 256\n (+ (choose (+ 2 (choice-index x)))\n (* 256\n (+ (choose (+ 1 (choice-index x)))\n (* 256\n (choose (+ 0 (choice-index x)))\n )\n )\n )\n )\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 4)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 4)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u32-le ((x State)) Result\n (mk-result\n (+ (choose (+ 0 (choice-index x)))\n (* 256\n (+ (choose (+ 1 (choice-index x)))\n (* 256\n (+ (choose (+ 2 (choice-index x)))\n (* 256\n (choose (+ 3 (choice-index x)))\n )\n )\n )\n )\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 4)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 4)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u64-be ((x State)) Result\n (mk-result\n (+ (choose (+ 7 (choice-index x)))\n (* 256\n (+ (choose (+ 6 (choice-index x)))\n (* 256\n (+ (choose (+ 5 (choice-index x)))\n (* 256\n (+ (choose (+ 4 (choice-index x)))\n (* 256\n (+ (choose (+ 3 (choice-index x)))\n (* 256\n (+ (choose (+ 2 (choice-index x)))\n (* 256\n (+ (choose (+ 1 (choice-index x)))\n (* 256\n (choose (+ 0 (choice-index x)))\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 8)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 8)\n (branch-index x)\n )\n )\n)\n\n(define-fun parse-u64-le ((x State)) Result\n (mk-result\n (+ (choose (+ 0 (choice-index x)))\n (* 256\n (+ (choose (+ 1 (choice-index x)))\n (* 256\n (+ (choose (+ 2 (choice-index x)))\n (* 256\n (+ (choose (+ 3 (choice-index x)))\n (* 256\n (+ (choose (+ 4 (choice-index x)))\n (* 256\n (+ (choose (+ 5 (choice-index x)))\n (* 256\n (+ (choose (+ 6 (choice-index x)))\n (* 256\n (choose (+ 7 (choice-index x)))\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n )\n (mk-state\n (let ((new-size (- (input-size x) 8)))\n (if (< new-size 0)\n -1\n new-size\n )\n )\n (+ (choice-index x) 8)\n (branch-index x)\n )\n )\n)\n\n(define-fun-rec pow-2 ((amount Int)) Int\n (if (<= amount 0)\n 1\n (* 2 (pow-2 (- amount 1)))\n )\n)\n\n;; see LowParse.BitFields.get_bitfield_eq\n(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int\n (mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))\n)\n\n;; see EverParse3d.Prelude.StaticHeader\n(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int\n (get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))\n)\n\n;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size\n(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State\n (if (< (input-size x) 0)\n x\n (if (and (= 0 (mod size eltSize)) (>= (input-size x) size))\n (mk-state\n (- (input-size x) size)\n (+ (choice-index x) size)\n (branch-index x)\n )\n (mk-state\n -1\n (choice-index x)\n (branch-index x)\n )\n )\n )\n)\n\n(declare-const initial-input-size Int)\n(assert (>= initial-input-size 0))\n(define-fun initial-state () State (mk-state initial-input-size 0 0))\n\n" | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_correct | val parse_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k)
(parse_vldata_gen l (in_bounds min max) p)) | val parse_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k)
(parse_vldata_gen l (in_bounds min max) p)) | let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 54,
"end_line": 312,
"start_col": 0,
"start_line": 283
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
p: LowParse.Spec.Base.parser k t
-> FStar.Pervasives.Lemma
(ensures
LowParse.Spec.Base.parser_kind_prop (LowParse.Spec.VLData.parse_bounded_vldata_strong_kind min
max
l
k)
(LowParse.Spec.VLData.parse_vldata_gen l (LowParse.Spec.BoundedInt.in_bounds min max) p)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"FStar.Classical.forall_intro",
"LowParse.Bytes.bytes",
"Prims.l_imp",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"FStar.Pervasives.Native.__proj__Some__item__v",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"Prims.logical",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowParse.Spec.VLData.parse_bounded_vldata_elim'",
"FStar.Pervasives.Native.option",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.Base.get_parser_kind",
"LowParse.Spec.VLData.parse_vldata_gen_kind",
"LowParse.Spec.VLData.parse_vldata_gen",
"LowParse.Spec.BoundedInt.in_bounds",
"LowParse.Spec.BoundedInt.integer_size",
"Prims.l_True",
"LowParse.Spec.Base.parser_kind_prop"
] | [] | false | false | true | false | false | let parse_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k)
(parse_vldata_gen l (in_bounds min max) p)) =
| parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k)
(parse_vldata_gen l (in_bounds min max) p);
let sz:integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf (input: bytes)
: Lemma (requires (Some? (parse p' input)))
(ensures
(let pi = parse p' input in
Some? pi /\
(let Some (_, consumed) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high))) =
let Some (data, consumed) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf) | false |
Hacl.Impl.Blake2.Generic.fst | Hacl.Impl.Blake2.Generic.blake2_mixing | val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y))) | val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y))) | let blake2_mixing #al #m wv x y =
let h0 = ST.get() in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@inline_let]
let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@inline_let]
let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@inline_let]
let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@inline_let]
let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get() in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get() in
pop_frame ();
let h3 = ST.get() in
assert(modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y)) | {
"file_name": "code/blake2/Hacl.Impl.Blake2.Generic.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 105,
"end_line": 186,
"start_col": 0,
"start_line": 154
} | module Hacl.Impl.Blake2.Generic
open FStar.Mul
open FStar.HyperStack
open FStar.HyperStack.ST
open Lib.IntTypes
open Lib.Buffer
open Lib.ByteBuffer
open Lib.LoopCombinators
module ST = FStar.HyperStack.ST
module Seq = Lib.Sequence
module Loops = Lib.LoopCombinators
module Spec = Spec.Blake2
open Hacl.Impl.Blake2.Constants
open Hacl.Impl.Blake2.Core
#set-options "--z3rlimit 50 --max_ifuel 0 --max_fuel 0"
noextract
let is_valid_blake2_config (a : Spec.alg) (m : m_spec) =
match a, m with
| Spec.Blake2S, M32 | Spec.Blake2S, M128
| Spec.Blake2B, M32 | Spec.Blake2B, M256 -> true
| _ -> false
inline_for_extraction noextract
let valid_m_spec (a : Spec.alg) = m:m_spec{is_valid_blake2_config a m}
/// Accessors for constants
inline_for_extraction noextract
val get_iv:
a:Spec.alg
-> s: size_t{size_v s < 8} ->
Stack (word_t a)
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\
v z == v (Seq.index (Spec.ivTable a) (v s))))
let get_iv a s =
recall_contents #(Spec.pub_word_t Spec.Blake2S) #8ul ivTable_S (Spec.ivTable Spec.Blake2S);
recall_contents #(Spec.pub_word_t Spec.Blake2B) #8ul ivTable_B (Spec.ivTable Spec.Blake2B);
[@inline_let]
let ivTable: (x:glbuffer (Spec.pub_word_t a) 8ul{witnessed x (Spec.ivTable a) /\ recallable x}) =
match a with
| Spec.Blake2S -> ivTable_S
| Spec.Blake2B -> ivTable_B
in
let r = index ivTable s in
secret #(Spec.wt a) r
inline_for_extraction noextract
val get_sigma:
s: size_t{v s < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ z == Lib.Sequence.(Spec.sigmaTable.[v s])))
let get_sigma s =
recall_contents sigmaTable Spec.sigmaTable;
index sigmaTable s
inline_for_extraction noextract
val get_sigma_sub:
start: size_t ->
i: size_t{v i < 16 /\ v start + v i < 160} ->
Stack Spec.sigma_elt_t
(requires (fun h -> True))
(ensures (fun h0 z h1 -> h0 == h1 /\ v z == v (Seq.index Spec.sigmaTable (v start + v i))))
let get_sigma_sub start i = get_sigma (start +. i)
inline_for_extraction noextract
let rounds_t (a:Spec.alg): size_t = size (Spec.rounds a)
inline_for_extraction noextract
val size_to_word: al:Spec.alg -> s:size_t -> u:word_t al{u == Spec.nat_to_word al (v s)}
let size_to_word al s = match al with
| Spec.Blake2S -> size_to_uint32 s
| Spec.Blake2B -> size_to_uint64 s
inline_for_extraction noextract
val size_to_limb: al:Spec.alg -> s:size_t -> u:Spec.limb_t al{u == Spec.nat_to_limb al (v s)}
let size_to_limb al s = match al with
| Spec.Blake2S -> size_to_uint64 s
| Spec.Blake2B -> to_u128 (size_to_uint64 s)
/// Constants
/// Define algorithm functions
inline_for_extraction noextract
val g1: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> r:rotval (Spec.wt al) ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ (state_v h1 wv) == Spec.g1 al (state_v h0 wv) (v a) (v b) r))
let g1 #al #m wv a b r =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
xor_row wv_a wv_b;
ror_row wv_a r;
let h2 = ST.get() in
Lib.Sequence.eq_intro (state_v h2 wv) (Spec.g1 al (state_v h0 wv) (v a) (v b) r)
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t -> x:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ disjoint wv x /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x)))
let g2 #al #m wv a b x =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
add_row wv_a x;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2 al (state_v h0 wv) (v a) (v b) (row_v h0 x))
#push-options "--z3rlimit 100 --max_fuel 1 --max_ifuel 1"
inline_for_extraction noextract
val g2z: #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> a:index_t -> b:index_t ->
Stack unit
(requires (fun h -> live h wv /\ a <> b))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.g2z al (state_v h0 wv) (v a) (v b)))
let g2z #al #m wv a b =
let h0 = ST.get() in
let wv_a = rowi wv a in
let wv_b = rowi wv b in
add_row wv_a wv_b;
let h1 = ST.get() in
Lib.Sequence.eq_intro (state_v h1 wv) (Spec.g2z al (state_v h0 wv) (v a) (v b))
inline_for_extraction noextract
val blake2_mixing : #al:Spec.alg -> #m:m_spec -> wv:state_p al m -> x:row_p al m -> y:row_p al m ->
Stack unit
(requires (fun h -> live h wv /\ live h x /\ live h y /\ disjoint wv x /\ disjoint wv y))
(ensures (fun h0 _ h1 -> modifies (loc wv) h0 h1
/\ state_v h1 wv == Spec.blake2_mixing al (state_v h0 wv) (row_v h0 x) (row_v h0 y))) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.ByteBuffer.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"Hacl.Impl.Blake2.Constants.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Blake2.Generic.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Constants",
"short_module": null
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteBuffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
wv: Hacl.Impl.Blake2.Core.state_p al m ->
x: Hacl.Impl.Blake2.Core.row_p al m ->
y: Hacl.Impl.Blake2.Core.row_p al m
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Spec.Blake2.Definitions.alg",
"Hacl.Impl.Blake2.Core.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"Hacl.Impl.Blake2.Core.row_p",
"Lib.Sequence.eq_intro",
"Spec.Blake2.Definitions.row",
"Hacl.Impl.Blake2.Core.state_v",
"Spec.Blake2.blake2_mixing",
"Hacl.Impl.Blake2.Core.row_v",
"Prims.unit",
"Prims._assert",
"Lib.Buffer.modifies",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"Hacl.Impl.Blake2.Core.element_t",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.pop_frame",
"Hacl.Impl.Blake2.Generic.g1",
"Hacl.Impl.Blake2.Generic.g2z",
"Hacl.Impl.Blake2.Generic.g2",
"FStar.Pervasives.normalize_term_spec",
"Lib.IntTypes.rotval",
"Spec.Blake2.Definitions.wt",
"Prims.eq2",
"FStar.Seq.Base.index",
"Lib.Sequence.to_seq",
"Spec.Blake2.rTable",
"Lib.Sequence.index",
"FStar.Pervasives.normalize_term",
"FStar.UInt32.t",
"FStar.UInt32.__uint_to_t",
"FStar.HyperStack.ST.push_frame"
] | [] | false | true | false | false | false | let blake2_mixing #al #m wv x y =
| let h0 = ST.get () in
push_frame ();
let a = 0ul in
let b = 1ul in
let c = 2ul in
let d = 3ul in
[@@ inline_let ]let r0 = normalize_term (Lib.Sequence.index (Spec.rTable al) 0) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 0);
[@@ inline_let ]let r1 = normalize_term (Lib.Sequence.index (Spec.rTable al) 1) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 1);
[@@ inline_let ]let r2 = normalize_term (Lib.Sequence.index (Spec.rTable al) 2) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 2);
[@@ inline_let ]let r3 = normalize_term (Lib.Sequence.index (Spec.rTable al) 3) in
normalize_term_spec (Lib.Sequence.index (Spec.rTable al) 3);
let h1 = ST.get () in
g2 wv a b x;
g1 wv d a r0;
g2z wv c d;
g1 wv b c r1;
g2 wv a b y;
g1 wv d a r2;
g2z wv c d;
g1 wv b c r3;
let h2 = ST.get () in
pop_frame ();
let h3 = ST.get () in
assert (modifies (loc wv) h0 h3);
Lib.Sequence.eq_intro (state_v h2 wv)
(Spec.blake2_mixing al (state_v h1 wv) (row_v h1 x) (row_v h1 y)) | false |
Z3TestGen.fst | Z3TestGen.is_bitwise_op | val is_bitwise_op (x: T.op) : Tot (option A.integer_type) | val is_bitwise_op (x: T.op) : Tot (option A.integer_type) | let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 13,
"end_line": 332,
"start_col": 0,
"start_line": 323
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Target.op -> FStar.Pervasives.Native.option Ast.integer_type | Prims.Tot | [
"total"
] | [] | [
"Target.op",
"Ast.integer_type",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.None",
"FStar.Pervasives.Native.option"
] | [] | false | false | false | true | false | let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
| match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a -> Some a
| _ -> None | false |
Z3TestGen.fst | Z3TestGen.mk_bitwise_op | val mk_bitwise_op (op: string) (bitvec_args: option string) : ML string | val mk_bitwise_op (op: string) (bitvec_args: option string) : ML string | let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args)) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 48,
"end_line": 335,
"start_col": 0,
"start_line": 334
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | op: Prims.string -> bitvec_args: FStar.Pervasives.Native.option Prims.string
-> FStar.All.ML Prims.string | FStar.All.ML | [
"ml"
] | [] | [
"Prims.string",
"FStar.Pervasives.Native.option",
"Z3TestGen.mk_app",
"FStar.Pervasives.Native.Some"
] | [] | false | true | false | false | false | let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
| mk_app "bv2int" (Some (mk_app op bitvec_args)) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux | val serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s)) | val serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s)) | let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 491,
"start_col": 0,
"start_line": 472
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
s: LowParse.Spec.Base.serializer p
-> LowParse.Spec.Base.bare_serializer (LowParse.Spec.VLData.parse_bounded_vldata_strong_t min
max
s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"FStar.Seq.Base.append",
"LowParse.Bytes.byte",
"Prims.unit",
"LowParse.Spec.Combinators.seq_slice_append_r",
"LowParse.Spec.Combinators.seq_slice_append_l",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.BoundedInt.serialize_bounded_integer",
"FStar.UInt32.t",
"FStar.UInt32.uint_to_t",
"Prims._assert",
"FStar.Seq.Base.length",
"LowParse.Spec.Base.bare_serializer"
] | [] | false | false | false | false | false | let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s)) =
| (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl) | false |
Z3TestGen.fst | Z3TestGen.mk_bitwise_arg | val mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string | val mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string | let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 78,
"end_line": 377,
"start_col": 0,
"start_line": 376
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Ast.integer_type -> arg: Prims.string -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Ast.integer_type",
"Prims.string",
"Z3TestGen.mk_app",
"Prims.op_Hat",
"Prims.string_of_int",
"Z3TestGen.integer_type_bit_size",
"FStar.Pervasives.Native.Some"
] | [] | false | false | false | true | false | let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
| mk_app ("(_ int2bv " ^ string_of_int (integer_type_bit_size t) ^ ")") (Some arg) | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong | val serialize_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s)) | val serialize_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s)) | let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 630,
"start_col": 0,
"start_line": 622
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s: LowParse.Spec.Base.serializer p
-> LowParse.Spec.Base.serializer (LowParse.Spec.VLData.parse_bounded_vldata_strong min max s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong'",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.VLData.parse_bounded_vldata_strong"
] | [] | false | false | false | false | false | let serialize_bounded_vldata_strong
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s)) =
| serialize_bounded_vldata_strong' min max (log256' max) s | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata' | val parse_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t) | val parse_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t) | let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 102,
"end_line": 323,
"start_col": 0,
"start_line": 314
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
p: LowParse.Spec.Base.parser k t
-> LowParse.Spec.Base.parser (LowParse.Spec.VLData.parse_bounded_vldata_strong_kind min max l k) t | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.strengthen",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.VLData.parse_vldata_gen",
"LowParse.Spec.BoundedInt.in_bounds",
"Prims.unit",
"LowParse.Spec.VLData.parse_bounded_vldata_correct"
] | [] | false | false | false | false | false | let parse_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t) =
| parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p) | false |
Z3TestGen.fst | Z3TestGen.ident_to_string | val ident_to_string : i: Ast.with_meta_t Ast.ident' -> Prims.string | let ident_to_string = A.ident_to_string | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 39,
"end_line": 374,
"start_col": 0,
"start_line": 374
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: Ast.with_meta_t Ast.ident' -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Ast.ident_to_string"
] | [] | false | false | false | true | false | let ident_to_string =
| A.ident_to_string | false |
|
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_precond | val serialize_bounded_vldata_precond
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(k: parser_kind)
: GTot bool | val serialize_bounded_vldata_precond
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(k: parser_kind)
: GTot bool | let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 56,
"end_line": 639,
"start_col": 0,
"start_line": 632
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
k: LowParse.Spec.Base.parser_kind
-> Prims.GTot Prims.bool | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_high",
"Prims.op_AmpAmp",
"LowParse.Spec.Base.__proj__Mkparser_kind'__item__parser_kind_low",
"Prims.bool"
] | [] | false | false | false | false | false | let serialize_bounded_vldata_precond
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(k: parser_kind)
: GTot bool =
| match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong' | val serialize_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s)) | val serialize_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s)) | let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 49,
"end_line": 620,
"start_col": 0,
"start_line": 610
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
s: LowParse.Spec.Base.serializer p
-> LowParse.Spec.Base.serializer (LowParse.Spec.VLData.parse_bounded_vldata_strong' min max l s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux",
"Prims.unit",
"FStar.Classical.forall_intro",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_bounded_vldata_strong'",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_correct",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind"
] | [] | false | false | false | false | false | let serialize_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s)) =
| Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_vldata_gen_correct_aux | val serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2 /\ Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == Seq.length b)))) | val serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2 /\ Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == Seq.length b)))) | let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 535,
"start_col": 0,
"start_line": 493
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t ->
b: LowParse.Bytes.bytes ->
b1: LowParse.Bytes.bytes ->
b2: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
FStar.Seq.Base.length b1 == sz /\
(let vlen =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) b1
in
Some? vlen /\
(let _ = vlen in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ len _) =
_
in
f len == true /\ FStar.Seq.Base.length b2 == FStar.UInt32.v len /\
(let vv = LowParse.Spec.Base.parse p b2 in
Some? vv /\
(let _ = vv in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ _ consumed) =
_
in
consumed == FStar.Seq.Base.length b2 /\
FStar.Seq.Base.length b1 <= FStar.Seq.Base.length b /\
FStar.Seq.Base.slice b 0 (FStar.Seq.Base.length b1) == b1 /\
FStar.Seq.Base.slice b (FStar.Seq.Base.length b1) (FStar.Seq.Base.length b) ==
b2)
<:
Prims.logical)))
<:
Prims.logical)))
(ensures
(let vv = LowParse.Spec.Base.parse p b2 in
Some? vv /\
(let _ = vv in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ v consumed)
=
_
in
let vv' = LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen sz f p) b in
Some? vv' /\
(let _ = vv' in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ v' consumed') =
_
in
v == v' /\ consumed == FStar.Seq.Base.length b2 /\
consumed' == FStar.Seq.Base.length b)
<:
Prims.logical))
<:
Prims.logical))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.VLData.parse_vldata_gen_eq",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"FStar.Seq.Base.slice",
"Prims.op_Addition",
"FStar.UInt32.v",
"Prims.int",
"FStar.Seq.Base.length",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"LowParse.Spec.Base.injective_postcond",
"LowParse.Spec.Base.no_lookahead_on",
"Prims.nat",
"Prims.l_or",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.l_and",
"LowParse.Spec.Base.parser_kind_prop_equiv",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"FStar.Pervasives.Native.uu___is_Some",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.logical",
"Prims.squash",
"LowParse.Spec.VLData.parse_vldata_gen",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2 /\ Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == Seq.length b)))) =
| let Some (len, consumed1) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_elim_forall | val parse_bounded_vldata_elim_forall
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma (requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures
(let Some (x, consumed) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | val parse_bounded_vldata_elim_forall
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma (requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures
(let Some (x, consumed) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) | let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 57,
"end_line": 392,
"start_col": 0,
"start_line": 364
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
p: LowParse.Spec.Base.parser k t ->
xbytes: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
Some? (LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata' min max l p)
xbytes))
(ensures
(let _ =
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata' min max l p) xbytes
in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ x consumed) =
_
in
let sz = l in
let plen =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) xbytes
in
Some? plen /\
(let _ = plen in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ len consumed_len) =
_
in
consumed_len == sz /\ LowParse.Spec.BoundedInt.in_bounds min max len /\
FStar.UInt32.v len <= FStar.Seq.Base.length xbytes - sz /\
(let input' = FStar.Seq.Base.slice xbytes sz (sz + FStar.UInt32.v len) in
let pp = LowParse.Spec.Base.parse p input' in
Some? pp /\
(let _ = pp in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ x' consumed_p) =
_
in
x' == x /\ consumed_p == FStar.UInt32.v len /\
consumed == sz + FStar.UInt32.v len)
<:
Prims.logical)))
<:
Prims.logical))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.VLData.parse_bounded_vldata_elim",
"Prims.unit",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_bounded_vldata'",
"FStar.Pervasives.Native.uu___is_Some",
"Prims.squash",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.eq2",
"LowParse.Spec.BoundedInt.in_bounds",
"FStar.UInt32.v",
"Prims.op_Subtraction",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.op_Addition",
"Prims.logical",
"FStar.Seq.Base.seq",
"FStar.Seq.Base.slice",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.BoundedInt.integer_size",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma (requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures
(let Some (x, consumed) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\
(let Some (len, consumed_len) = plen in
(consumed_len <: nat) == (sz <: nat) /\ in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\
(let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\
(let Some (x', consumed_p) = pp in
x' == x /\ (consumed_p <: nat) == U32.v len /\ (consumed <: nat) == sz + U32.v len))
))) =
| let Some (x, consumed) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_vldata_gen_correct | val serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == sz + Seq.length b2)))) | val serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == sz + Seq.length b2)))) | let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2 | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 66,
"end_line": 570,
"start_col": 0,
"start_line": 537
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
sz: LowParse.Spec.BoundedInt.integer_size ->
f: (_: LowParse.Spec.BoundedInt.bounded_integer sz -> Prims.GTot Prims.bool) ->
p: LowParse.Spec.Base.parser k t ->
b1: LowParse.Bytes.bytes ->
b2: LowParse.Bytes.bytes
-> FStar.Pervasives.Lemma
(requires
FStar.Seq.Base.length b1 == sz /\
(let vlen =
LowParse.Spec.Base.parse (LowParse.Spec.BoundedInt.parse_bounded_integer sz) b1
in
Some? vlen /\
(let _ = vlen in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ len _) =
_
in
f len == true /\ FStar.Seq.Base.length b2 == FStar.UInt32.v len /\
(let vv = LowParse.Spec.Base.parse p b2 in
Some? vv /\
(let _ = vv in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ _ consumed) =
_
in
consumed == FStar.Seq.Base.length b2)
<:
Prims.logical)))
<:
Prims.logical)))
(ensures
(let vv = LowParse.Spec.Base.parse p b2 in
Some? vv /\
(let _ = vv in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ v consumed)
=
_
in
let vv' =
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_vldata_gen sz f p)
(FStar.Seq.Base.append b1 b2)
in
Some? vv' /\
(let _ = vv' in
(let
FStar.Pervasives.Native.Some
#_
(FStar.Pervasives.Native.Mktuple2 #_ #_ v' consumed') =
_
in
v == v' /\ consumed == FStar.Seq.Base.length b2 /\
consumed' == sz + FStar.Seq.Base.length b2)
<:
Prims.logical))
<:
Prims.logical))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.bool",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Bytes.bytes",
"LowParse.Spec.VLData.serialize_vldata_gen_correct_aux",
"FStar.Seq.Base.append",
"LowParse.Bytes.byte",
"Prims.unit",
"LowParse.Spec.Combinators.seq_slice_append_r",
"LowParse.Spec.Combinators.seq_slice_append_l",
"Prims.l_and",
"Prims.eq2",
"Prims.nat",
"FStar.Seq.Base.length",
"Prims.b2t",
"FStar.Pervasives.Native.uu___is_Some",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.consumed_length",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt32.v",
"Prims.logical",
"FStar.Pervasives.Native.option",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"Prims.squash",
"Prims.op_Addition",
"LowParse.Spec.VLData.parse_vldata_gen",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires
(Seq.length b1 == sz /\
(let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\
(let Some (len, _) = vlen in
f len == true /\ Seq.length b2 == U32.v len /\
(let vv = parse p b2 in
Some? vv /\
(let Some (_, consumed) = vv in
consumed == Seq.length b2))))))
(ensures
(let vv = parse p b2 in
Some? vv /\
(let Some (v, consumed) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\
(let Some (v', consumed') = vv' in
v == v' /\ consumed == Seq.length b2 /\ consumed' == sz + Seq.length b2)))) =
| seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2 | false |
Z3TestGen.fst | Z3TestGen.push_binder | val push_binder (name typ: string) (b: binders) : binders | val push_binder (name typ: string) (b: binders) : binders | let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
} | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 417,
"start_col": 0,
"start_line": 413
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
} | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | name: Prims.string -> typ: Prims.string -> b: Z3TestGen.binders -> Z3TestGen.binders | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Z3TestGen.binders",
"Z3TestGen.Mkbinders",
"FStar.Printf.sprintf",
"Z3TestGen.__proj__Mkbinders__item__bind",
"Z3TestGen.__proj__Mkbinders__item__args"
] | [] | false | false | false | true | false | let push_binder (name typ: string) (b: binders) : binders =
| {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args
} | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_strong' | val parse_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s)
) | val parse_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s)
) | let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 460,
"start_col": 0,
"start_line": 446
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
s: LowParse.Spec.Base.serializer p
-> LowParse.Spec.Base.parser (LowParse.Spec.VLData.parse_bounded_vldata_strong_kind min max l k)
(LowParse.Spec.VLData.parse_bounded_vldata_strong_t min max s) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.Base.coerce_parser",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_pred",
"LowParse.Spec.Combinators.parse_strengthen",
"LowParse.Spec.VLData.parse_bounded_vldata'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_correct"
] | [] | false | false | false | false | false | let parse_bounded_vldata_strong'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot
(parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s)
) =
| (coerce_parser (parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p)
(parse_bounded_vldata_strong_pred min max s)
(parse_bounded_vldata_strong_correct min max l s))) | false |
Z3TestGen.fst | Z3TestGen.mk_function_call | val mk_function_call : name: Prims.string -> b: Z3TestGen.binders -> Prims.string | let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 420,
"start_col": 0,
"start_line": 419
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
} | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | name: Prims.string -> b: Z3TestGen.binders -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Z3TestGen.binders",
"FStar.Printf.sprintf",
"Z3TestGen.__proj__Mkbinders__item__args"
] | [] | false | false | false | true | false | let mk_function_call (name: string) (b: binders) =
| Printf.sprintf "%s%s" name b.args | false |
|
Z3TestGen.fst | Z3TestGen.empty_binders | val empty_binders:binders | val empty_binders:binders | let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
} | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 411,
"start_col": 0,
"start_line": 407
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
} | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Z3TestGen.binders | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.Mkbinders"
] | [] | false | false | false | true | false | let empty_binders:binders =
| { is_empty = true; bind = ""; args = "" } | false |
Z3TestGen.fst | Z3TestGen.parse_readable_itype | val parse_readable_itype (i: I.readable_itype) : Tot (parser reading) | val parse_readable_itype (i: I.readable_itype) : Tot (parser reading) | let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 65,
"end_line": 448,
"start_col": 0,
"start_line": 447
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | i: InterpreterTarget.readable_itype -> Z3TestGen.parser Z3TestGen.reading | Prims.Tot | [
"total"
] | [] | [
"InterpreterTarget.readable_itype",
"Z3TestGen.leaf_reading_parser",
"Prims.op_Hat",
"Z3TestGen.readable_itype_parser_suffix",
"Z3TestGen.parser",
"Z3TestGen.reading"
] | [] | false | false | false | true | false | let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
| leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i) | false |
Z3TestGen.fst | Z3TestGen.unsupported_parser | val unsupported_parser (s: string) (a: Type) : Tot (parser a) | val unsupported_parser (s: string) (a: Type) : Tot (parser a) | let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 69,
"end_line": 430,
"start_col": 0,
"start_line": 429
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Prims.string -> a: Type -> Z3TestGen.parser a | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"FStar.All.failwith",
"FStar.Printf.sprintf",
"Z3TestGen.parser"
] | [] | false | false | false | true | false | let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
| fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s) | false |
Z3TestGen.fst | Z3TestGen.mk_wrap_parser | val mk_wrap_parser (name binders body: string) : string | val mk_wrap_parser (name binders body: string) : string | let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 460,
"start_col": 0,
"start_line": 450
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | name: Prims.string -> binders: Prims.string -> body: Prims.string -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.op_Hat",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let mk_wrap_parser (name binders body: string) : string =
| let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun " ^
name ^
" (" ^ binders ^ "(" ^ input ^ " State)) State\n (after-state (" ^ body ^ " " ^ input ^ "))\n )\n" | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.parse_bounded_vldata_strong_correct | val parse_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x)) | val parse_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x)) | let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
() | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 444,
"start_col": 0,
"start_line": 418
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } ) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
s: LowParse.Spec.Base.serializer p ->
xbytes: LowParse.Bytes.bytes ->
consumed: LowParse.Spec.Base.consumed_length xbytes ->
x: t
-> FStar.Pervasives.Lemma
(requires
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata' min max l p) xbytes ==
FStar.Pervasives.Native.Some (x, consumed))
(ensures LowParse.Spec.VLData.parse_bounded_vldata_strong_pred min max s x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.BoundedInt.bounded_integer",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"LowParse.Spec.Base.serializer_correct_implies_complete",
"FStar.Seq.Base.equal",
"FStar.Seq.Base.slice",
"FStar.UInt32.v",
"Prims.op_Addition",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"Prims.l_True",
"Prims.squash",
"FStar.Pervasives.Native.uu___is_Some",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowParse.Spec.VLData.parse_bounded_vldata_elim",
"LowParse.Spec.Base.parse",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.VLData.parse_bounded_vldata'",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_pred"
] | [] | false | false | true | false | false | let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma (requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x)) =
| parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz:integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) = parse_bounded_vldata_elim min max l p xbytes x consumed in
f ();
let Some (len, _) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
() | false |
Z3TestGen.fst | Z3TestGen.leaf_reading_parser | val leaf_reading_parser (name: string) : parser reading | val leaf_reading_parser (name: string) : parser reading | let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name } | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 433,
"start_col": 0,
"start_line": 432
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | name: Prims.string -> Z3TestGen.parser Z3TestGen.reading | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mkreading",
"Z3TestGen.reading",
"Z3TestGen.parser"
] | [] | false | false | false | true | false | let leaf_reading_parser (name: string) : parser reading =
| fun _ _ _ _ -> { call = name } | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong_correct | val serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted ==
Some (input, Seq.length formatted)) | val serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted ==
Some (input, Seq.length formatted)) | let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
() | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 608,
"start_col": 0,
"start_line": 572
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
l: Prims.nat{l >= LowParse.Spec.BoundedInt.log256' max /\ l <= 4} ->
s: LowParse.Spec.Base.serializer p ->
input: LowParse.Spec.VLData.parse_bounded_vldata_strong_t min max s
-> FStar.Pervasives.Lemma
(ensures
(let formatted = LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux min max l s input in
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata_strong' min max l s)
formatted ==
FStar.Pervasives.Native.Some (input, FStar.Seq.Base.length formatted))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"Prims.op_GreaterThanOrEqual",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.Base.consumed_length",
"Prims.unit",
"LowParse.Spec.VLData.serialize_vldata_gen_correct",
"LowParse.Spec.BoundedInt.in_bounds",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.uu___is_Some",
"LowParse.Spec.Base.parse",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.UInt32.v",
"Prims.bool",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.serialize_bounded_integer",
"Prims.pow2",
"FStar.Mul.op_Star",
"FStar.Math.Lemmas.pow2_le_compat",
"FStar.UInt32.t",
"FStar.UInt32.uint_to_t",
"Prims.l_True",
"Prims.squash",
"LowParse.Spec.VLData.parse_bounded_vldata_strong'",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(l: nat{l >= log256' max /\ l <= 4})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted ==
Some (input, Seq.length formatted)) =
| let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let len:bounded_integer sz = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let Some (len', consumed_len') = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let Some (_, consumed_p) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p slen sp;
() | false |
Z3TestGen.fst | Z3TestGen.maybe_toplevel_parser | val maybe_toplevel_parser (p: parser not_reading) : parser not_reading | val maybe_toplevel_parser (p: parser not_reading) : parser not_reading | let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 33,
"end_line": 489,
"start_col": 0,
"start_line": 480
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Z3TestGen.parser Z3TestGen.not_reading -> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.parser",
"Z3TestGen.not_reading",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.mk_function_call",
"Z3TestGen.mk_toplevel_parser",
"Z3TestGen.__proj__Mkbinders__item__bind",
"Z3TestGen.__proj__Mknot_reading__item__call",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
| fun name binders is_toplevel out ->
if is_toplevel
then
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
else p name binders false out | false |
Z3TestGen.fst | Z3TestGen.mk_constant | val mk_constant : _: Ast.constant -> FStar.All.ALL Prims.string | let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 27,
"end_line": 313,
"start_col": 0,
"start_line": 308
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Ast.constant -> FStar.All.ALL Prims.string | FStar.All.ALL | [] | [] | [
"Ast.constant",
"Prims.string",
"Ast.integer_type",
"Prims.int",
"Prims.string_of_int",
"OS.int_of_string"
] | [] | false | true | false | false | false | let mk_constant =
| function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false" | false |
|
Z3TestGen.fst | Z3TestGen.mk_toplevel_parser | val mk_toplevel_parser (name binders body: string) : string | val mk_toplevel_parser (name binders body: string) : string | let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 478,
"start_col": 0,
"start_line": 469
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders } | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | name: Prims.string -> binders: Prims.string -> body: Prims.string -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.op_Hat",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let mk_toplevel_parser (name binders body: string) : string =
| let input = Printf.sprintf "%s-input" name in
"(define-fun " ^
name ^ " (" ^ binders ^ "(" ^ input ^ " State)) State\n (" ^ body ^ " " ^ input ^ ")\n )\n" | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_correct | val serialize_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: Lemma
(let Some (_, consumed) = parse p (serialize s x) in
let y =
serialize_bounded_vldata_strong_aux min
max
(log256' max)
s
(x <: parse_bounded_vldata_strong_t min max s)
in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)) | val serialize_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: Lemma
(let Some (_, consumed) = parse p (serialize s x) in
let y =
serialize_bounded_vldata_strong_aux min
max
(log256' max)
s
(x <: parse_bounded_vldata_strong_t min max s)
in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)) | let serialize_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: Lemma
( let Some (_, consumed) = parse p (serialize s x) in
let y = serialize_bounded_vldata_strong_aux min max (log256' max) s (x <: parse_bounded_vldata_strong_t min max s) in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y))
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
() | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 4,
"end_line": 655,
"start_col": 0,
"start_line": 641
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s:
LowParse.Spec.Base.serializer p
{LowParse.Spec.VLData.serialize_bounded_vldata_precond min max k} ->
x: t
-> FStar.Pervasives.Lemma
(ensures
(let _ = LowParse.Spec.Base.parse p (LowParse.Spec.Base.serialize s x) in
(let FStar.Pervasives.Native.Some #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ _ _) = _ in
let y =
LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux min
max
(LowParse.Spec.BoundedInt.log256' max)
s
x
in
LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata min max p) y ==
FStar.Pervasives.Native.Some (x, FStar.Seq.Base.length y))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.serialize_bounded_vldata_precond",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.serialize",
"Prims.unit",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_correct",
"LowParse.Spec.BoundedInt.log256'",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"LowParse.Spec.VLData.parse_bounded_vldata",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"LowParse.Bytes.bytes",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let serialize_bounded_vldata_correct
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: Lemma
(let Some (_, consumed) = parse p (serialize s x) in
let y =
serialize_bounded_vldata_strong_aux min
max
(log256' max)
s
(x <: parse_bounded_vldata_strong_t min max s)
in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)) =
| let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
() | false |
Z3TestGen.fst | Z3TestGen.wrap_parser | val wrap_parser (p: parser reading) : parser not_reading | val wrap_parser (p: parser reading) : parser not_reading | let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders } | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 44,
"end_line": 467,
"start_col": 0,
"start_line": 462
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Z3TestGen.parser Z3TestGen.reading -> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.parser",
"Z3TestGen.reading",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.mk_function_call",
"Z3TestGen.not_reading",
"Z3TestGen.mk_wrap_parser",
"Z3TestGen.__proj__Mkbinders__item__bind",
"Z3TestGen.__proj__Mkreading__item__call",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let wrap_parser (p: parser reading) : parser not_reading =
| fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders } | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata | val serialize_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
: Tot (serializer (parse_bounded_vldata min max p)) | val serialize_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
: Tot (serializer (parse_bounded_vldata min max p)) | let serialize_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
: Tot (serializer (parse_bounded_vldata min max p))
= serialize_bounded_vldata' min max s | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 37,
"end_line": 678,
"start_col": 0,
"start_line": 670
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max
let serialize_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: Lemma
( let Some (_, consumed) = parse p (serialize s x) in
let y = serialize_bounded_vldata_strong_aux min max (log256' max) s (x <: parse_bounded_vldata_strong_t min max s) in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y))
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
()
let serialize_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: GTot (y: bytes { parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y) } )
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_correct min max s x;
serialize_bounded_vldata_strong_aux min max (log256' max) s x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s:
LowParse.Spec.Base.serializer p
{LowParse.Spec.VLData.serialize_bounded_vldata_precond min max k}
-> LowParse.Spec.Base.serializer (LowParse.Spec.VLData.parse_bounded_vldata min max p) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.serialize_bounded_vldata_precond",
"LowParse.Spec.VLData.serialize_bounded_vldata'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata"
] | [] | false | false | false | false | false | let serialize_bounded_vldata
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
: Tot (serializer (parse_bounded_vldata min max p)) =
| serialize_bounded_vldata' min max s | false |
Z3TestGen.fst | Z3TestGen.mk_app | val mk_app : fn: Prims.string -> _: FStar.Pervasives.Native.option Prims.string -> Prims.string | let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 49,
"end_line": 317,
"start_col": 0,
"start_line": 315
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | fn: Prims.string -> _: FStar.Pervasives.Native.option Prims.string -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"FStar.Pervasives.Native.option",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let mk_app fn =
| function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args | false |
|
Z3TestGen.fst | Z3TestGen.parse_all_bytes | val parse_all_bytes:parser not_reading | val parse_all_bytes:parser not_reading | let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" }) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 69,
"end_line": 492,
"start_col": 0,
"start_line": 491
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.maybe_toplevel_parser",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.not_reading"
] | [] | false | false | false | true | false | let parse_all_bytes:parser not_reading =
| maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" }) | false |
Z3TestGen.fst | Z3TestGen.parse_all_zeros | val parse_all_zeros:parser not_reading | val parse_all_zeros:parser not_reading | let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" }) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 69,
"end_line": 495,
"start_col": 0,
"start_line": 494
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" }) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.maybe_toplevel_parser",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.not_reading"
] | [] | false | false | false | true | false | let parse_all_zeros:parser not_reading =
| maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" }) | false |
Z3TestGen.fst | Z3TestGen.assert_some | val assert_some : _: FStar.Pervasives.Native.option _ -> FStar.All.ALL _ | let assert_some = function
| None -> failwith "assert_some"
| Some x -> x | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 15,
"end_line": 321,
"start_col": 0,
"start_line": 319
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: FStar.Pervasives.Native.option _ -> FStar.All.ALL _ | FStar.All.ALL | [] | [] | [
"FStar.Pervasives.Native.option",
"FStar.All.failwith"
] | [] | false | true | false | false | false | let assert_some =
| function
| None -> failwith "assert_some"
| Some x -> x | false |
|
Z3TestGen.fst | Z3TestGen.mk_app_without_paren' | val mk_app_without_paren' : id: Prims.string -> args: Prims.list Target.expr -> FStar.All.ML Prims.string | let mk_app_without_paren' id args =
mk_args_aux None id args | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 26,
"end_line": 503,
"start_col": 0,
"start_line": 502
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | id: Prims.string -> args: Prims.list Target.expr -> FStar.All.ML Prims.string | FStar.All.ML | [
"ml"
] | [] | [
"Prims.string",
"Prims.list",
"Target.expr",
"Z3TestGen.mk_args_aux",
"FStar.Pervasives.Native.None",
"Ast.integer_type"
] | [] | false | true | false | false | false | let mk_app_without_paren' id args =
| mk_args_aux None id args | false |
|
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata' | val serialize_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: GTot (y: bytes{parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)}) | val serialize_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: GTot (y: bytes{parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)}) | let serialize_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: GTot (y: bytes { parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y) } )
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_correct min max s x;
serialize_bounded_vldata_strong_aux min max (log256' max) s x | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 63,
"end_line": 668,
"start_col": 0,
"start_line": 657
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max
let serialize_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: Lemma
( let Some (_, consumed) = parse p (serialize s x) in
let y = serialize_bounded_vldata_strong_aux min max (log256' max) s (x <: parse_bounded_vldata_strong_t min max s) in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y))
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s:
LowParse.Spec.Base.serializer p
{LowParse.Spec.VLData.serialize_bounded_vldata_precond min max k} ->
x: t
-> Prims.GTot
(y:
LowParse.Bytes.bytes
{ LowParse.Spec.Base.parse (LowParse.Spec.VLData.parse_bounded_vldata min max p) y ==
FStar.Pervasives.Native.Some (x, FStar.Seq.Base.length y) }) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.serialize_bounded_vldata_precond",
"LowParse.Spec.Base.consumed_length",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_aux",
"LowParse.Spec.BoundedInt.log256'",
"Prims.unit",
"LowParse.Spec.VLData.serialize_bounded_vldata_correct",
"LowParse.Bytes.bytes",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"LowParse.Spec.Base.parse",
"LowParse.Spec.VLData.parse_bounded_vldata",
"FStar.Pervasives.Native.Some",
"FStar.Pervasives.Native.Mktuple2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte"
] | [] | false | false | false | false | false | let serialize_bounded_vldata'
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p {serialize_bounded_vldata_precond min max k})
(x: t)
: GTot (y: bytes{parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y)}) =
| let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_correct min max s x;
serialize_bounded_vldata_strong_aux min max (log256' max) s x | false |
Z3TestGen.fst | Z3TestGen.parse_not_readable_app | val parse_not_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser not_reading) | val parse_not_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser not_reading) | let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 51,
"end_line": 531,
"start_col": 0,
"start_line": 527
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args }) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hd: Ast.ident -> args: Prims.list InterpreterTarget.expr -> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Ast.ident",
"Prims.list",
"InterpreterTarget.expr",
"Z3TestGen.parse_not_readable_app'",
"Z3TestGen.ident_to_string",
"Z3TestGen.parser",
"Z3TestGen.not_reading"
] | [] | false | false | false | true | false | let parse_not_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser not_reading) =
| parse_not_readable_app' (ident_to_string hd) args | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong_upd | val serialize_bounded_vldata_strong_upd
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_seq sx lm sy)) | val serialize_bounded_vldata_strong_upd
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_seq sx lm sy)) | let serialize_bounded_vldata_strong_upd
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma
(requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures (
let sy = serialize s y in
let y : parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_seq sx lm sy
))
= let y : parse_bounded_vldata_strong_t min max s = y in
let sx = serialize s x in
let sx' = serialize (serialize_bounded_vldata_strong min max s) x in
let sy = serialize s y in
let sy' = serialize (serialize_bounded_vldata_strong min max s) y in
let lm = log256' max in
let sz = serialize (serialize_bounded_integer lm) (U32.uint_to_t (Seq.length sx)) in
assert (lm + Seq.length sy == Seq.length sx');
seq_upd_seq_right sx' sy;
Seq.lemma_split sx' lm;
Seq.lemma_split sy' lm;
Seq.lemma_append_inj (Seq.slice sx' 0 lm) (Seq.slice sx' lm (Seq.length sx')) sz sx;
Seq.lemma_append_inj (Seq.slice sy' 0 lm) (Seq.slice sy' lm (Seq.length sy')) sz sy;
assert (sy' `Seq.equal` seq_upd_seq sx' lm sy) | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 50,
"end_line": 712,
"start_col": 0,
"start_line": 680
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max
let serialize_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: Lemma
( let Some (_, consumed) = parse p (serialize s x) in
let y = serialize_bounded_vldata_strong_aux min max (log256' max) s (x <: parse_bounded_vldata_strong_t min max s) in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y))
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
()
let serialize_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: GTot (y: bytes { parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y) } )
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_correct min max s x;
serialize_bounded_vldata_strong_aux min max (log256' max) s x
let serialize_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
: Tot (serializer (parse_bounded_vldata min max p))
= serialize_bounded_vldata' min max s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s: LowParse.Spec.Base.serializer p ->
x: LowParse.Spec.VLData.parse_bounded_vldata_strong_t min max s ->
y: t
-> FStar.Pervasives.Lemma
(requires
FStar.Seq.Base.length (LowParse.Spec.Base.serialize s y) ==
FStar.Seq.Base.length (LowParse.Spec.Base.serialize s x))
(ensures
(let sy = LowParse.Spec.Base.serialize s y in
let y = y in
let sx =
LowParse.Spec.Base.serialize (LowParse.Spec.VLData.serialize_bounded_vldata_strong min
max
s)
x
in
let lm = LowParse.Spec.BoundedInt.log256' max in
lm + FStar.Seq.Base.length sy == FStar.Seq.Base.length sx /\
LowParse.Spec.Base.serialize (LowParse.Spec.VLData.serialize_bounded_vldata_strong min
max
s)
y ==
LowParse.Spec.Base.seq_upd_seq sx lm sy)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"Prims._assert",
"FStar.Seq.Base.equal",
"LowParse.Bytes.byte",
"LowParse.Spec.Base.seq_upd_seq",
"Prims.unit",
"FStar.Seq.Properties.lemma_append_inj",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.length",
"FStar.Seq.Properties.lemma_split",
"LowParse.Spec.Base.seq_upd_seq_right",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.BoundedInt.parse_bounded_integer_kind",
"LowParse.Spec.BoundedInt.bounded_integer",
"LowParse.Spec.BoundedInt.parse_bounded_integer",
"LowParse.Spec.BoundedInt.serialize_bounded_integer",
"FStar.UInt32.uint_to_t",
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.VLData.parse_bounded_vldata_strong",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong",
"Prims.squash",
"FStar.Seq.Base.seq",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let serialize_bounded_vldata_strong_upd
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_seq sx lm sy)) =
| let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize s x in
let sx' = serialize (serialize_bounded_vldata_strong min max s) x in
let sy = serialize s y in
let sy' = serialize (serialize_bounded_vldata_strong min max s) y in
let lm = log256' max in
let sz = serialize (serialize_bounded_integer lm) (U32.uint_to_t (Seq.length sx)) in
assert (lm + Seq.length sy == Seq.length sx');
seq_upd_seq_right sx' sy;
Seq.lemma_split sx' lm;
Seq.lemma_split sy' lm;
Seq.lemma_append_inj (Seq.slice sx' 0 lm) (Seq.slice sx' lm (Seq.length sx')) sz sx;
Seq.lemma_append_inj (Seq.slice sy' 0 lm) (Seq.slice sy' lm (Seq.length sy')) sz sy;
assert (sy' `Seq.equal` (seq_upd_seq sx' lm sy)) | false |
Z3TestGen.fst | Z3TestGen.mk_app_without_paren | val mk_app_without_paren : id: Ast.with_meta_t Ast.ident' -> args: Prims.list Target.expr -> FStar.All.ML Prims.string | let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 49,
"end_line": 506,
"start_col": 0,
"start_line": 505
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | id: Ast.with_meta_t Ast.ident' -> args: Prims.list Target.expr -> FStar.All.ML Prims.string | FStar.All.ML | [
"ml"
] | [] | [
"Ast.with_meta_t",
"Ast.ident'",
"Prims.list",
"Target.expr",
"Z3TestGen.mk_app_without_paren'",
"Z3TestGen.ident_to_string",
"Prims.string"
] | [] | false | true | false | false | false | let mk_app_without_paren id args =
| mk_app_without_paren' (ident_to_string id) args | false |
|
Z3TestGen.fst | Z3TestGen.parse_readable_app | val parse_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser reading) | val parse_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser reading) | let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args } | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 56,
"end_line": 512,
"start_col": 0,
"start_line": 508
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hd: Ast.ident -> args: Prims.list InterpreterTarget.expr -> Z3TestGen.parser Z3TestGen.reading | Prims.Tot | [
"total"
] | [] | [
"Ast.ident",
"Prims.list",
"InterpreterTarget.expr",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mkreading",
"Z3TestGen.reading",
"Z3TestGen.mk_app_without_paren",
"Z3TestGen.parser"
] | [] | false | false | false | true | false | let parse_readable_app (hd: A.ident) (args: list I.expr) : Tot (parser reading) =
| fun _ _ _ _ -> { call = mk_app_without_paren hd args } | false |
Z3TestGen.fst | Z3TestGen.parse_denoted | val parse_denoted (d: I.dtyp) : parser not_reading | val parse_denoted (d: I.dtyp) : parser not_reading | let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 14,
"end_line": 546,
"start_col": 0,
"start_line": 545
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" }) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | d: InterpreterTarget.dtyp -> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"InterpreterTarget.dtyp",
"Z3TestGen.parse_dtyp",
"Z3TestGen.parser",
"Z3TestGen.not_reading"
] | [] | false | false | false | true | false | let parse_denoted (d: I.dtyp) : parser not_reading =
| parse_dtyp d | false |
Z3TestGen.fst | Z3TestGen.integer_type_bit_size | val integer_type_bit_size : _: Ast.integer_type -> Prims.int | let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64 | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 16,
"end_line": 341,
"start_col": 0,
"start_line": 337
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args)) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Ast.integer_type -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Ast.integer_type",
"Prims.int"
] | [] | false | false | false | true | false | let integer_type_bit_size =
| function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64 | false |
|
Z3TestGen.fst | Z3TestGen.parse_not_readable_app' | val parse_not_readable_app' (hd: string) (args: list I.expr) : Tot (parser not_reading) | val parse_not_readable_app' (hd: string) (args: list I.expr) : Tot (parser not_reading) | let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args }) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 81,
"end_line": 525,
"start_col": 0,
"start_line": 521
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hd: Prims.string -> args: Prims.list InterpreterTarget.expr
-> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.list",
"InterpreterTarget.expr",
"Z3TestGen.maybe_toplevel_parser",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.not_reading",
"Z3TestGen.mk_app_without_paren'",
"Z3TestGen.parser"
] | [] | false | false | false | true | false | let parse_not_readable_app' (hd: string) (args: list I.expr) : Tot (parser not_reading) =
| maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args }) | false |
Z3TestGen.fst | Z3TestGen.mk_maybe_bitwise_arg | val mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string | val mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string | let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 382,
"start_col": 0,
"start_line": 379
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg) | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: FStar.Pervasives.Native.option Ast.integer_type -> arg: Prims.string -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.option",
"Ast.integer_type",
"Prims.string",
"Z3TestGen.mk_bitwise_arg"
] | [] | false | false | false | true | false | let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
| match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg | false |
LowParse.Spec.VLData.fsti | LowParse.Spec.VLData.serialize_bounded_vldata_strong_upd_bw | val serialize_bounded_vldata_strong_upd_bw
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_bw_seq sx 0 sy)) | val serialize_bounded_vldata_strong_upd_bw
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_bw_seq sx 0 sy)) | let serialize_bounded_vldata_strong_upd_bw
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma
(requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures (
let sy = serialize s y in
let y : parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_bw_seq sx 0 sy
))
= serialize_bounded_vldata_strong_upd min max s x y | {
"file_name": "src/lowparse/LowParse.Spec.VLData.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 51,
"end_line": 733,
"start_col": 0,
"start_line": 714
} | module LowParse.Spec.VLData
include LowParse.Spec.FLData
include LowParse.Spec.AllIntegers // for bounded_integer, in_bounds, etc.
module Seq = FStar.Seq
module U32 = FStar.UInt32
module M = LowParse.Math
#reset-options "--z3rlimit 64 --max_fuel 64 --max_ifuel 64 --z3refresh --z3cliopt smt.arith.nl=false"
let parse_vldata_payload_size
(sz: integer_size)
: Pure nat
(requires True)
(ensures (fun y -> y == pow2 (FStar.Mul.op_Star 8 sz) - 1 ))
= match sz with
| 1 -> 255
| 2 -> 65535
| 3 -> 16777215
| 4 -> 4294967295
#reset-options
// unfold
let parse_vldata_payload_kind
(sz: integer_size)
(k: parser_kind)
: parser_kind
= strong_parser_kind 0 (parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_payload
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(i: bounded_integer sz { f i == true } )
: Tot (parser (parse_vldata_payload_kind sz k) t)
= weaken (parse_vldata_payload_kind sz k) (parse_fldata p (U32.v i))
#set-options "--z3rlimit 64"
let parse_fldata_and_then_cases_injective
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p))
= parser_kind_prop_equiv k p;
let g
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1 b2: bytes)
: Lemma
(requires (and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2))
(ensures (len1 == len2))
= assert (injective_precond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (injective_postcond p (Seq.slice b1 0 (U32.v len1)) (Seq.slice b2 0 (U32.v len2)));
assert (len1 == len2)
in
let g'
(len1 len2: (len: bounded_integer sz { f len == true } ))
(b1: bytes)
: Lemma
(forall (b2: bytes) . and_then_cases_injective_precond (parse_vldata_payload sz f p) len1 len2 b1 b2 ==> len1 == len2)
= Classical.forall_intro (Classical.move_requires (g len1 len2 b1))
in
Classical.forall_intro_3 g'
#reset-options
// unfold
let parse_vldata_gen_kind
(sz: integer_size)
(k: parser_kind)
: Tot parser_kind
= strong_parser_kind sz (sz + parse_vldata_payload_size sz) (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_vldata_gen_kind_correct
(sz: integer_size)
(k: parser_kind)
: Lemma
( (parse_vldata_gen_kind sz k) == (and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k)))
= let kl = parse_vldata_gen_kind sz k in
let kr = and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) in
assert_norm (kl == kr)
val parse_vldata_gen
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_vldata_gen_kind sz k) t)
val parse_vldata_gen_eq_def
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(and_then_cases_injective (parse_vldata_payload sz f p) /\
parse_vldata_gen_kind sz k == and_then_kind (parse_filter_kind (parse_bounded_integer_kind sz)) (parse_vldata_payload_kind sz k) /\
parse_vldata_gen sz f p ==
and_then
#_
#(parse_filter_refine #(bounded_integer sz) f)
(parse_filter #_ #(bounded_integer sz) (parse_bounded_integer sz) f)
#_
#t
(parse_vldata_payload sz f p))
let parse_vldata_gen_eq
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(let res = parse (parse_vldata_gen sz f p) input in
match parse (parse_bounded_integer sz) input with
| None -> res == None
| Some (len, consumed_len) ->
consumed_len == sz /\ (
if f len
then begin
if Seq.length input < sz + U32.v len
then res == None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then res == Some (x, sz + U32.v len)
else res == None
| _ -> res == None
end
else res == None
))
= parse_vldata_gen_eq_def sz f p;
and_then_eq #_ #(parse_filter_refine f) (parse_filter (parse_bounded_integer sz) f) #_ #t (parse_vldata_payload sz f p) input;
parse_filter_eq #_ #(bounded_integer sz) (parse_bounded_integer sz) f input;
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
()
let parse_vldata_gen_eq_some_elim
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(requires (Some? (parse (parse_vldata_gen sz f p) input)))
(ensures (
let pbi = parse (parse_bounded_integer sz) input in
Some? pbi /\ (
let Some (len, consumed_len) = pbi in
consumed_len == sz /\
f len /\
Seq.length input >= sz + U32.v len /\ (
let input' = Seq.slice input sz (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let Some (x, consumed_x) = pp in
consumed_x = U32.v len /\
parse (parse_vldata_gen sz f p) input == Some (x, sz + U32.v len)
)))))
= parse_vldata_gen_eq sz f p input
let unconstrained_bounded_integer
(sz: integer_size)
(i: bounded_integer sz)
: GTot bool
= true
let parse_vldata
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser _ t)
= parse_vldata_gen sz (unconstrained_bounded_integer sz) p
let parse_vldata_eq
(sz: integer_size)
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(input: bytes)
: Lemma
(parse (parse_vldata sz p) input == (match parse (parse_bounded_integer sz) input with
| None -> None
| Some (len, _) ->
begin
if Seq.length input < sz + U32.v len
then None
else
let input' = Seq.slice input sz (sz + U32.v len) in
match parse p input' with
| Some (x, consumed_x) ->
if consumed_x = U32.v len
then Some (x, sz + U32.v len)
else None
| _ -> None
end
))
= parse_vldata_gen_eq sz (unconstrained_bounded_integer _) p input
(** Explicit bounds on size *)
#reset-options
inline_for_extraction
let parse_bounded_vldata_strong_kind
(min: nat)
(max: nat)
(l: nat)
(k: parser_kind)
: Pure parser_kind
(requires (min <= max /\ max > 0 /\ max < 4294967296 /\ l >= log256' max /\ l <= 4 ))
(ensures (fun _ -> True))
= [@inline_let]
let kmin = k.parser_kind_low in
[@inline_let]
let min' = if kmin > min then kmin else min in
[@inline_let]
let max' = match k.parser_kind_high with
| None -> max
| Some kmax -> if kmax < max then kmax else max
in
[@inline_let]
let max' = if max' < min' then min' else max' in
(* the size of the length prefix must conform to the max bound given by the user, not on the metadata *)
strong_parser_kind (l + min') (l + max') (
match k.parser_kind_metadata with
| Some ParserKindMetadataFail -> Some ParserKindMetadataFail
| _ -> None
)
let parse_bounded_vldata_elim'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_vldata_gen l (in_bounds min max) p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_vldata_gen_eq l (in_bounds min max) p xbytes;
parser_kind_prop_equiv (parse_bounded_integer_kind l) (parse_bounded_integer l)
let parse_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Lemma
(parser_kind_prop (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p))
= parser_kind_prop_equiv (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p);
let sz : integer_size = l in
let p' = parse_vldata_gen sz (in_bounds min max) p in
parser_kind_prop_equiv (get_parser_kind p') p';
parser_kind_prop_equiv k p;
let k' = parse_bounded_vldata_strong_kind min max l k in
let prf
(input: bytes)
: Lemma
(requires (Some? (parse p' input)))
(ensures (
let pi = parse p' input in
Some? pi /\ (
let (Some (_, consumed)) = pi in
k'.parser_kind_low <= (consumed <: nat) /\
(consumed <: nat) <= Some?.v k'.parser_kind_high
)))
= let (Some (data, consumed)) = parse p' input in
parse_bounded_vldata_elim' min max l p input data consumed
in
Classical.forall_intro (Classical.move_requires prf)
let parse_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) t)
= parse_bounded_vldata_correct min max l p;
strengthen (parse_bounded_vldata_strong_kind min max l k) (parse_vldata_gen l (in_bounds min max) p)
let parse_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) t)
= parse_bounded_vldata' min max (log256' max) p
let parse_bounded_vldata_elim
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
(x: t)
(consumed: consumed_length xbytes)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= parse_bounded_vldata_elim' min max l p xbytes x consumed
let parse_bounded_vldata_elim_forall
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(xbytes: bytes)
: Lemma
(requires (Some? (parse (parse_bounded_vldata' min max l p) xbytes)))
(ensures (
let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
Some? plen /\ (
let (Some (len, consumed_len)) = plen in
(consumed_len <: nat) == (sz <: nat) /\
in_bounds min max len /\
U32.v len <= Seq.length xbytes - sz /\ (
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
let pp = parse p input' in
Some? pp /\ (
let (Some (x', consumed_p)) = pp in
x' == x /\
(consumed_p <: nat) == U32.v len /\
(consumed <: nat) == sz + U32.v len
)))))
= let (Some (x, consumed)) = parse (parse_bounded_vldata' min max l p) xbytes in
parse_bounded_vldata_elim min max l p xbytes x consumed
(* Serialization *)
let parse_bounded_vldata_strong_pred
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: t)
: GTot Type0
= let reslen = Seq.length (s x) in
min <= reslen /\ reslen <= max
let parse_bounded_vldata_strong_t
(min: nat)
(max: nat)
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot Type
= (x: t { parse_bounded_vldata_strong_pred min max s x } )
let parse_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(xbytes: bytes)
(consumed: consumed_length xbytes)
(x: t)
: Lemma
(requires (parse (parse_bounded_vldata' min max l p) xbytes == Some (x, consumed)))
(ensures (parse_bounded_vldata_strong_pred min max s x))
= parse_bounded_vldata_elim min max l p xbytes x consumed;
let sz : integer_size = l in
let plen = parse (parse_bounded_integer sz) xbytes in
let f () : Lemma (Some? plen) =
parse_bounded_vldata_elim min max l p xbytes x consumed
in
f ();
let (Some (len, _)) = plen in
let input' = Seq.slice xbytes (sz <: nat) (sz + U32.v len) in
assert (Seq.equal input' (Seq.slice input' 0 (U32.v len)));
serializer_correct_implies_complete p s;
assert (s x == input');
()
let parse_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max l k) (parse_bounded_vldata_strong_t min max s))
= // strengthen (parse_bounded_vldata_strong_kind min max k)
(
coerce_parser
(parse_bounded_vldata_strong_t min max s)
(parse_strengthen (parse_bounded_vldata' min max l p) (parse_bounded_vldata_strong_pred min max s) (parse_bounded_vldata_strong_correct min max l s))
)
let parse_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (parser (parse_bounded_vldata_strong_kind min max (log256' max) k) (parse_bounded_vldata_strong_t min max s))
= parse_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_strong_aux
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (bare_serializer (parse_bounded_vldata_strong_t min max s))
= (fun (x: parse_bounded_vldata_strong_t min max s) ->
let pl = s x in
let sz = l in
let nlen = Seq.length pl in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
let slen = serialize (serialize_bounded_integer sz) len in
seq_slice_append_l slen pl;
seq_slice_append_r slen pl;
Seq.append slen pl
)
let serialize_vldata_gen_correct_aux
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2 /\
Seq.length b1 <= Seq.length b /\
Seq.slice b 0 (Seq.length b1) == b1 /\
Seq.slice b (Seq.length b1) (Seq.length b) == b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) b in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == Seq.length b
)))) =
let (Some (len, consumed1)) = parse (parse_bounded_integer sz) b1 in
parser_kind_prop_equiv (parse_bounded_integer_kind sz) (parse_bounded_integer sz);
assert (consumed1 == sz);
assert (no_lookahead_on (parse_bounded_integer sz) b1 b);
assert (injective_postcond (parse_bounded_integer sz) b1 b);
assert (parse (parse_bounded_integer sz) b == Some (len, sz));
assert (sz + U32.v len == Seq.length b);
assert (b2 == Seq.slice b sz (sz + U32.v len));
parse_vldata_gen_eq sz f p b
let serialize_vldata_gen_correct
(sz: integer_size)
(f: (bounded_integer sz -> GTot bool))
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(b1 b2: bytes)
: Lemma
(requires (
Seq.length b1 == sz /\ (
let vlen = parse (parse_bounded_integer sz) b1 in
Some? vlen /\ (
let (Some (len, _)) = vlen in
f len == true /\
Seq.length b2 == U32.v len /\ (
let vv = parse p b2 in
Some? vv /\ (
let (Some (_, consumed)) = vv in
consumed == Seq.length b2
))))))
(ensures (
let vv = parse p b2 in
Some? vv /\ (
let (Some (v, consumed)) = vv in
let vv' = parse (parse_vldata_gen sz f p) (Seq.append b1 b2) in
Some? vv' /\ (
let (Some (v', consumed')) = vv' in
v == v' /\
consumed == Seq.length b2 /\
consumed' == sz + Seq.length b2
)))) =
seq_slice_append_l b1 b2;
seq_slice_append_r b1 b2;
serialize_vldata_gen_correct_aux sz f p (Seq.append b1 b2) b1 b2
let serialize_bounded_vldata_strong_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(input: parse_bounded_vldata_strong_t min max s)
: Lemma
(let formatted = serialize_bounded_vldata_strong_aux min max l s input in
parse (parse_bounded_vldata_strong' min max l s) formatted == Some (input, Seq.length formatted))
= let sz = l in
let sp = serialize s input in
let nlen = Seq.length sp in
assert (min <= nlen /\ nlen <= max);
let len = U32.uint_to_t nlen in
M.pow2_le_compat (FStar.Mul.op_Star 8 sz) (FStar.Mul.op_Star 8 (log256' max));
assert (U32.v len < pow2 (FStar.Mul.op_Star 8 sz));
let (len: bounded_integer sz) = len in
let slen = serialize (serialize_bounded_integer sz) len in
assert (Seq.length slen == sz);
let pslen = parse (parse_bounded_integer sz) slen in
assert (Some? pslen);
let (Some (len', consumed_len')) = pslen in
assert (len == len');
assert (in_bounds min max len' == true);
assert (Seq.length sp == U32.v len);
let psp = parse p sp in
assert (Some? psp);
let (Some (_, consumed_p)) = psp in
assert ((consumed_p <: nat) == Seq.length sp);
serialize_vldata_gen_correct sz (in_bounds min max) p
slen
sp
;
()
let serialize_bounded_vldata_strong'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(l: nat { l >= log256' max /\ l <= 4 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong' min max l s))
= Classical.forall_intro (serialize_bounded_vldata_strong_correct min max l s);
serialize_bounded_vldata_strong_aux min max l s
let serialize_bounded_vldata_strong
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
: Tot (serializer (parse_bounded_vldata_strong min max s))
= serialize_bounded_vldata_strong' min max (log256' max) s
let serialize_bounded_vldata_precond
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(k: parser_kind)
: GTot bool
= match k.parser_kind_high with
| None -> false
| Some max' -> min <= k.parser_kind_low && max' <= max
let serialize_bounded_vldata_correct
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: Lemma
( let Some (_, consumed) = parse p (serialize s x) in
let y = serialize_bounded_vldata_strong_aux min max (log256' max) s (x <: parse_bounded_vldata_strong_t min max s) in
parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y))
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_strong_correct min max (log256' max) s x;
()
let serialize_bounded_vldata'
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
(x: t)
: GTot (y: bytes { parse (parse_bounded_vldata min max p) y == Some (x, Seq.length y) } )
= let Some (_, consumed) = parse p (serialize s x) in
serialize_bounded_vldata_correct min max s x;
serialize_bounded_vldata_strong_aux min max (log256' max) s x
let serialize_bounded_vldata
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p { serialize_bounded_vldata_precond min max k } )
: Tot (serializer (parse_bounded_vldata min max p))
= serialize_bounded_vldata' min max s
let serialize_bounded_vldata_strong_upd
(min: nat)
(max: nat { min <= max /\ max > 0 /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma
(requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures (
let sy = serialize s y in
let y : parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_seq sx lm sy
))
= let y : parse_bounded_vldata_strong_t min max s = y in
let sx = serialize s x in
let sx' = serialize (serialize_bounded_vldata_strong min max s) x in
let sy = serialize s y in
let sy' = serialize (serialize_bounded_vldata_strong min max s) y in
let lm = log256' max in
let sz = serialize (serialize_bounded_integer lm) (U32.uint_to_t (Seq.length sx)) in
assert (lm + Seq.length sy == Seq.length sx');
seq_upd_seq_right sx' sy;
Seq.lemma_split sx' lm;
Seq.lemma_split sy' lm;
Seq.lemma_append_inj (Seq.slice sx' 0 lm) (Seq.slice sx' lm (Seq.length sx')) sz sx;
Seq.lemma_append_inj (Seq.slice sy' 0 lm) (Seq.slice sy' lm (Seq.length sy')) sz sy;
assert (sy' `Seq.equal` seq_upd_seq sx' lm sy) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.FLData.fst.checked",
"LowParse.Spec.AllIntegers.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.VLData.fsti"
} | [
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.AllIntegers",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.FLData",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max > 0 /\ max < 4294967296} ->
s: LowParse.Spec.Base.serializer p ->
x: LowParse.Spec.VLData.parse_bounded_vldata_strong_t min max s ->
y: t
-> FStar.Pervasives.Lemma
(requires
FStar.Seq.Base.length (LowParse.Spec.Base.serialize s y) ==
FStar.Seq.Base.length (LowParse.Spec.Base.serialize s x))
(ensures
(let sy = LowParse.Spec.Base.serialize s y in
let y = y in
let sx =
LowParse.Spec.Base.serialize (LowParse.Spec.VLData.serialize_bounded_vldata_strong min
max
s)
x
in
let lm = LowParse.Spec.BoundedInt.log256' max in
lm + FStar.Seq.Base.length sy == FStar.Seq.Base.length sx /\
LowParse.Spec.Base.serialize (LowParse.Spec.VLData.serialize_bounded_vldata_strong min
max
s)
y ==
LowParse.Spec.Base.seq_upd_bw_seq sx 0 sy)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_GreaterThan",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.Base.serializer",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_t",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong_upd",
"Prims.unit",
"Prims.eq2",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"LowParse.Spec.Base.serialize",
"Prims.squash",
"Prims.int",
"Prims.op_Addition",
"FStar.Seq.Base.seq",
"LowParse.Spec.VLData.parse_bounded_vldata_strong_kind",
"LowParse.Spec.BoundedInt.log256'",
"LowParse.Spec.VLData.parse_bounded_vldata_strong",
"LowParse.Spec.VLData.serialize_bounded_vldata_strong",
"LowParse.Spec.Base.seq_upd_bw_seq",
"LowParse.Spec.BoundedInt.integer_size",
"LowParse.Bytes.bytes",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let serialize_bounded_vldata_strong_upd_bw
(min: nat)
(max: nat{min <= max /\ max > 0 /\ max < 4294967296})
(#k: parser_kind)
(#t: Type)
(#p: parser k t)
(s: serializer p)
(x: parse_bounded_vldata_strong_t min max s)
(y: t)
: Lemma (requires (Seq.length (serialize s y) == Seq.length (serialize s x)))
(ensures
(let sy = serialize s y in
let y:parse_bounded_vldata_strong_t min max s = y in
let sx = serialize (serialize_bounded_vldata_strong min max s) x in
let lm = log256' max in
lm + Seq.length sy == Seq.length sx /\
serialize (serialize_bounded_vldata_strong min max s) y == seq_upd_bw_seq sx 0 sy)) =
| serialize_bounded_vldata_strong_upd min max s x y | false |
Z3TestGen.fst | Z3TestGen.parse_square | val parse_square (p: parser not_reading) : parser not_reading | val parse_square (p: parser not_reading) : parser not_reading | let parse_square (p: parser not_reading) : parser not_reading =
fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders } | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 44,
"end_line": 580,
"start_col": 0,
"start_line": 575
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" })
let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d
let mk_parse_pair
(name: string)
(binders: string)
(fst: string)
(snd: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^fst^" "^input^")))
(if (< (input-size "^tmp^") 0)
"^tmp^"
("^snd^" "^tmp^")
)
)
)
"
let parse_pair (fst: parser not_reading) (snd: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_fst = Printf.sprintf "%s-fst" name in
let body_fst = fst name_fst binders false out in
let name_snd = Printf.sprintf "%s-snd" name in
let body_snd = snd name_snd binders false out in
out (mk_parse_pair name binders.bind body_fst.call body_snd.call);
{ call = mk_function_call name binders } | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Z3TestGen.parser Z3TestGen.not_reading -> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.parser",
"Z3TestGen.not_reading",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.mk_function_call",
"Z3TestGen.mk_parse_pair",
"Z3TestGen.__proj__Mkbinders__item__bind",
"Z3TestGen.__proj__Mknot_reading__item__call",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let parse_square (p: parser not_reading) : parser not_reading =
| fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders } | false |
Z3TestGen.fst | Z3TestGen.parse_false | val parse_false:parser not_reading | val parse_false:parser not_reading | let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" }) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 65,
"end_line": 543,
"start_col": 0,
"start_line": 542
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.maybe_toplevel_parser",
"Prims.string",
"Z3TestGen.binders",
"Prims.bool",
"Prims.unit",
"Z3TestGen.Mknot_reading",
"Z3TestGen.not_reading"
] | [] | false | false | false | true | false | let parse_false:parser not_reading =
| maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" }) | false |
Z3TestGen.fst | Z3TestGen.parse_refine | val parse_refine (tag: parser reading) (cond_binder: A.ident) (cond: (unit -> ML string))
: parser not_reading | val parse_refine (tag: parser reading) (cond_binder: A.ident) (cond: (unit -> ML string))
: parser not_reading | let parse_refine (tag: parser reading) (cond_binder: A.ident) (cond: unit -> ML string) : parser not_reading =
parse_dep_pair_with_refinement tag cond_binder cond cond_binder (parse_itype I.Unit) | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 86,
"end_line": 667,
"start_col": 0,
"start_line": 666
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" })
let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d
let mk_parse_pair
(name: string)
(binders: string)
(fst: string)
(snd: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^fst^" "^input^")))
(if (< (input-size "^tmp^") 0)
"^tmp^"
("^snd^" "^tmp^")
)
)
)
"
let parse_pair (fst: parser not_reading) (snd: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_fst = Printf.sprintf "%s-fst" name in
let body_fst = fst name_fst binders false out in
let name_snd = Printf.sprintf "%s-snd" name in
let body_snd = snd name_snd binders false out in
out (mk_parse_pair name binders.bind body_fst.call body_snd.call);
{ call = mk_function_call name binders }
let parse_square (p: parser not_reading) : parser not_reading =
fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders }
let mk_parse_dep_pair_with_refinement
(name: string)
(binders: string)
(dfst: string)
(cond_binder_name: string)
(cond: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
let condtmp = Printf.sprintf "%s-condtmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^condtmp^" (let (("^cond_binder_name^" (return-value "^tmp^"))) "^cond^")))
(if (and "^condtmp^" (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 0)))
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^"
(mk-state
(input-size (after-state "^tmp^"))
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
(mk-state
(if (and (not "^condtmp^") (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 1))) -1 -2)
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
)
)
)
"
let parse_dep_pair_with_refinement_gen (tag: parser reading) (cond_binder: string) (cond: unit -> ML string) (payload_binder: string) (payload: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_tag = Printf.sprintf "%s-tag" name in
let body_tag = tag name_tag binders false out in
let binders' = push_binder payload_binder "Int" binders in (* TODO: support more types *)
let name_payload = Printf.sprintf "%s-payload" name in
let body_payload = payload name_payload binders' false out in
out (mk_parse_dep_pair_with_refinement name binders.bind body_tag.call cond_binder (cond ()) payload_binder body_payload.call);
{ call = mk_function_call name binders }
let parse_dep_pair_with_refinement (tag: parser reading) (cond_binder: A.ident) (cond: unit -> ML string) (payload_binder: A.ident) (payload: parser not_reading) : parser not_reading =
parse_dep_pair_with_refinement_gen tag (ident_to_string cond_binder) cond (ident_to_string payload_binder) payload
let mk_parse_dep_pair
(name: string)
(binders: string)
(dfst: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^" (after-state "^tmp^"))
)
)
)
)
"
let parse_dep_pair (tag: parser reading) (payload_binder: A.ident) (payload: parser not_reading) : parser not_reading =
fun name binders _ out ->
let payload_binder = ident_to_string payload_binder in
let name_tag = Printf.sprintf "%s-tag" name in
let body_tag = tag name_tag binders false out in
let binders' = push_binder payload_binder "Int" binders in (* TODO: support more types *)
let name_payload = Printf.sprintf "%s-payload" name in
let body_payload = payload name_payload binders' false out in
out (mk_parse_dep_pair name binders.bind body_tag.call payload_binder body_payload.call);
{ call = mk_function_call name binders } | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
tag: Z3TestGen.parser Z3TestGen.reading ->
cond_binder: Ast.ident ->
cond: (_: Prims.unit -> FStar.All.ML Prims.string)
-> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Z3TestGen.parser",
"Z3TestGen.reading",
"Ast.ident",
"Prims.unit",
"Prims.string",
"Z3TestGen.parse_dep_pair_with_refinement",
"Z3TestGen.parse_itype",
"InterpreterTarget.Unit",
"Z3TestGen.not_reading"
] | [] | false | false | false | false | false | let parse_refine (tag: parser reading) (cond_binder: A.ident) (cond: (unit -> ML string))
: parser not_reading =
| parse_dep_pair_with_refinement tag cond_binder cond cond_binder (parse_itype I.Unit) | false |
Z3TestGen.fst | Z3TestGen.parse_ifthenelse_cons | val parse_ifthenelse_cons
(cond: (unit -> ML string))
(pthen: parser not_reading)
(pelse: (int -> parser not_reading))
(counter: int)
: parser not_reading | val parse_ifthenelse_cons
(cond: (unit -> ML string))
(pthen: parser not_reading)
(pelse: (int -> parser not_reading))
(counter: int)
: parser not_reading | let parse_ifthenelse_cons (cond: unit -> ML string) (pthen: parser not_reading) (pelse: int -> parser not_reading) (counter: int) : parser not_reading =
fun name binders _ out ->
let name_then = Printf.sprintf "%s-then" name in
let body_then = pthen name_then binders false out in
let name_else = Printf.sprintf "%s-else" name in
let body_else = pelse (counter + 1) name_else binders false out in
out (mk_parse_ifthenelse_cons counter name binders.bind (cond ()) body_then.call body_else.call);
{ call = mk_function_call name binders } | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 44,
"end_line": 696,
"start_col": 0,
"start_line": 689
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" })
let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d
let mk_parse_pair
(name: string)
(binders: string)
(fst: string)
(snd: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^fst^" "^input^")))
(if (< (input-size "^tmp^") 0)
"^tmp^"
("^snd^" "^tmp^")
)
)
)
"
let parse_pair (fst: parser not_reading) (snd: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_fst = Printf.sprintf "%s-fst" name in
let body_fst = fst name_fst binders false out in
let name_snd = Printf.sprintf "%s-snd" name in
let body_snd = snd name_snd binders false out in
out (mk_parse_pair name binders.bind body_fst.call body_snd.call);
{ call = mk_function_call name binders }
let parse_square (p: parser not_reading) : parser not_reading =
fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders }
let mk_parse_dep_pair_with_refinement
(name: string)
(binders: string)
(dfst: string)
(cond_binder_name: string)
(cond: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
let condtmp = Printf.sprintf "%s-condtmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^condtmp^" (let (("^cond_binder_name^" (return-value "^tmp^"))) "^cond^")))
(if (and "^condtmp^" (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 0)))
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^"
(mk-state
(input-size (after-state "^tmp^"))
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
(mk-state
(if (and (not "^condtmp^") (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 1))) -1 -2)
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
)
)
)
"
let parse_dep_pair_with_refinement_gen (tag: parser reading) (cond_binder: string) (cond: unit -> ML string) (payload_binder: string) (payload: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_tag = Printf.sprintf "%s-tag" name in
let body_tag = tag name_tag binders false out in
let binders' = push_binder payload_binder "Int" binders in (* TODO: support more types *)
let name_payload = Printf.sprintf "%s-payload" name in
let body_payload = payload name_payload binders' false out in
out (mk_parse_dep_pair_with_refinement name binders.bind body_tag.call cond_binder (cond ()) payload_binder body_payload.call);
{ call = mk_function_call name binders }
let parse_dep_pair_with_refinement (tag: parser reading) (cond_binder: A.ident) (cond: unit -> ML string) (payload_binder: A.ident) (payload: parser not_reading) : parser not_reading =
parse_dep_pair_with_refinement_gen tag (ident_to_string cond_binder) cond (ident_to_string payload_binder) payload
let mk_parse_dep_pair
(name: string)
(binders: string)
(dfst: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^" (after-state "^tmp^"))
)
)
)
)
"
let parse_dep_pair (tag: parser reading) (payload_binder: A.ident) (payload: parser not_reading) : parser not_reading =
fun name binders _ out ->
let payload_binder = ident_to_string payload_binder in
let name_tag = Printf.sprintf "%s-tag" name in
let body_tag = tag name_tag binders false out in
let binders' = push_binder payload_binder "Int" binders in (* TODO: support more types *)
let name_payload = Printf.sprintf "%s-payload" name in
let body_payload = payload name_payload binders' false out in
out (mk_parse_dep_pair name binders.bind body_tag.call payload_binder body_payload.call);
{ call = mk_function_call name binders }
let parse_refine (tag: parser reading) (cond_binder: A.ident) (cond: unit -> ML string) : parser not_reading =
parse_dep_pair_with_refinement tag cond_binder cond cond_binder (parse_itype I.Unit)
let mk_parse_ifthenelse_cons
(counter: int)
(name: string)
(binders: string)
(cond: string)
(f_then: string)
(f_else: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(if (and "^cond^" (or (< (branch-index "^input^") 0) (= (branch-trace (branch-index "^input^")) "^string_of_int counter^")))
("^f_then^" (if (< (branch-index "^input^") 0) "^input^" (mk-state (input-size "^input^") (choice-index "^input^") (+ 1 (branch-index "^input^")))))
(if (not "^cond^")
("^f_else^" "^input^")
(mk-state -2 (choice-index "^input^") (+ (if (< (branch-index "^input^") 0) 0 1) (branch-index "^input^"))) ; this is a Z3 encoding artifact, not a parsing failure
)
)
)
" | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
cond: (_: Prims.unit -> FStar.All.ML Prims.string) ->
pthen: Z3TestGen.parser Z3TestGen.not_reading ->
pelse: (_: Prims.int -> Z3TestGen.parser Z3TestGen.not_reading) ->
counter: Prims.int
-> Z3TestGen.parser Z3TestGen.not_reading | Prims.Tot | [
"total"
] | [] | [
"Prims.unit",
"Prims.string",
"Z3TestGen.parser",
"Z3TestGen.not_reading",
"Prims.int",
"Z3TestGen.binders",
"Prims.bool",
"Z3TestGen.Mknot_reading",
"Z3TestGen.mk_function_call",
"Z3TestGen.mk_parse_ifthenelse_cons",
"Z3TestGen.__proj__Mkbinders__item__bind",
"Z3TestGen.__proj__Mknot_reading__item__call",
"Prims.op_Addition",
"FStar.Printf.sprintf"
] | [] | false | false | false | false | false | let parse_ifthenelse_cons
(cond: (unit -> ML string))
(pthen: parser not_reading)
(pelse: (int -> parser not_reading))
(counter: int)
: parser not_reading =
| fun name binders _ out ->
let name_then = Printf.sprintf "%s-then" name in
let body_then = pthen name_then binders false out in
let name_else = Printf.sprintf "%s-else" name in
let body_else = pelse (counter + 1) name_else binders false out in
out (mk_parse_ifthenelse_cons counter name binders.bind (cond ()) body_then.call body_else.call);
{ call = mk_function_call name binders } | false |
Z3TestGen.fst | Z3TestGen.mk_parse_dep_pair_with_refinement | val mk_parse_dep_pair_with_refinement
(name binders dfst cond_binder_name cond dsnd_binder_name dsnd: string)
: string | val mk_parse_dep_pair_with_refinement
(name binders dfst cond_binder_name cond dsnd_binder_name dsnd: string)
: string | let mk_parse_dep_pair_with_refinement
(name: string)
(binders: string)
(dfst: string)
(cond_binder_name: string)
(cond: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
let condtmp = Printf.sprintf "%s-condtmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^condtmp^" (let (("^cond_binder_name^" (return-value "^tmp^"))) "^cond^")))
(if (and "^condtmp^" (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 0)))
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^"
(mk-state
(input-size (after-state "^tmp^"))
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
(mk-state
(if (and (not "^condtmp^") (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 1))) -1 -2)
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
)
)
)
" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 619,
"start_col": 0,
"start_line": 582
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" })
let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d
let mk_parse_pair
(name: string)
(binders: string)
(fst: string)
(snd: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^fst^" "^input^")))
(if (< (input-size "^tmp^") 0)
"^tmp^"
("^snd^" "^tmp^")
)
)
)
"
let parse_pair (fst: parser not_reading) (snd: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_fst = Printf.sprintf "%s-fst" name in
let body_fst = fst name_fst binders false out in
let name_snd = Printf.sprintf "%s-snd" name in
let body_snd = snd name_snd binders false out in
out (mk_parse_pair name binders.bind body_fst.call body_snd.call);
{ call = mk_function_call name binders }
let parse_square (p: parser not_reading) : parser not_reading =
fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders } | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
name: Prims.string ->
binders: Prims.string ->
dfst: Prims.string ->
cond_binder_name: Prims.string ->
cond: Prims.string ->
dsnd_binder_name: Prims.string ->
dsnd: Prims.string
-> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.op_Hat",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let mk_parse_dep_pair_with_refinement
(name binders dfst cond_binder_name cond dsnd_binder_name dsnd: string)
: string =
| let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
let condtmp = Printf.sprintf "%s-condtmp" name in
"(define-fun " ^
name ^
" (" ^
binders ^
"(" ^
input ^
" State)) State\n (let ((" ^
tmp ^
" (" ^
dfst ^
" " ^
input ^
")))\n (if (< (input-size (after-state " ^
tmp ^
")) 0)\n (after-state " ^
tmp ^
")\n (let ((" ^
condtmp ^
" (let ((" ^
cond_binder_name ^
" (return-value " ^
tmp ^
"))) " ^
cond ^
")))\n (if (and " ^
condtmp ^
" (or (< (branch-index (after-state " ^
tmp ^
")) 0) (= (branch-trace (branch-index (after-state " ^
tmp ^
"))) 0)))\n (let ((" ^
dsnd_binder_name ^
" (return-value " ^
tmp ^
")))\n (" ^
dsnd ^
"\n (mk-state\n (input-size (after-state " ^
tmp ^
"))\n (choice-index (after-state " ^
tmp ^
"))\n (+ (if (< (branch-index (after-state " ^
tmp ^
")) 0) 0 1) (branch-index (after-state " ^
tmp ^
")))\n )\n )\n )\n (mk-state\n (if (and (not " ^
condtmp ^
") (or (< (branch-index (after-state " ^
tmp ^
")) 0) (= (branch-trace (branch-index (after-state " ^
tmp ^
"))) 1))) -1 -2)\n (choice-index (after-state " ^
tmp ^
"))\n (+ (if (< (branch-index (after-state " ^
tmp ^
")) 0) 0 1) (branch-index (after-state " ^
tmp ^ ")))\n )\n )\n )\n )\n )\n )\n" | false |
Z3TestGen.fst | Z3TestGen.mk_parse_dep_pair | val mk_parse_dep_pair (name binders dfst dsnd_binder_name dsnd: string) : string | val mk_parse_dep_pair (name binders dfst dsnd_binder_name dsnd: string) : string | let mk_parse_dep_pair
(name: string)
(binders: string)
(dfst: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^" (after-state "^tmp^"))
)
)
)
)
" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 653,
"start_col": 0,
"start_line": 634
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64
let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))"
let mk_op : T.op -> option string -> ML string = function
| T.Eq -> mk_app "="
| T.Neq -> (fun s -> mk_app "not" (Some (mk_app "=" s)))
| T.And -> mk_app "and"
| T.Or -> mk_app "or"
| T.Not -> mk_app "not"
| T.Plus _ -> mk_app "+"
| T.Minus _ -> mk_app "-"
| T.Mul _ -> mk_app "*"
| T.Division _ -> mk_app "div"
| T.Remainder _ -> mk_app "mod"
| T.BitwiseAnd _ -> mk_bitwise_op "bvand"
| T.BitwiseXor _ -> mk_bitwise_op "bvxor"
| T.BitwiseOr _ -> mk_bitwise_op "bvor"
| T.BitwiseNot a -> mk_bitwise_not a
| T.ShiftLeft _ -> mk_bitwise_op "bvshl"
| T.ShiftRight _ -> mk_bitwise_op "bvlshr"
| T.LT _ -> mk_app "<"
| T.GT _ -> mk_app ">"
| T.LE _ -> mk_app "<="
| T.GE _ -> mk_app ">="
| T.IfThenElse -> mk_app "if"
| T.BitFieldOf size order -> (fun arg -> Printf.sprintf "(get-bitfield-%ssb %d %s)" (match order with A.LSBFirst -> "l" | A.MSBFirst -> "m") size (assert_some arg))
| T.Cast _ _ -> assert_some (* casts allowed only if they are proven not to lose precision *)
| T.Ext s -> mk_app s
let ident_to_string = A.ident_to_string
let mk_bitwise_arg (t: A.integer_type) (arg: string) : Tot string =
mk_app ("(_ int2bv "^string_of_int (integer_type_bit_size t)^")") (Some arg)
let mk_maybe_bitwise_arg (t: option A.integer_type) (arg: string) : Tot string =
match t with
| None -> arg
| Some t -> mk_bitwise_arg t arg
let rec mk_expr (e: T.expr) : ML string = match fst e with
| T.Constant c -> mk_constant c
| T.Identifier i -> ident_to_string i
| T.App hd args -> mk_op hd (mk_args (is_bitwise_op hd) args)
| _ -> failwith "mk_expr: not supported"
and mk_args_aux (is_bitwise_op: option A.integer_type) accu : (list T.expr -> ML string) = function
| [] -> accu
| a :: q -> mk_args_aux is_bitwise_op (Printf.sprintf "%s %s" accu (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a))) q
and mk_args (is_bitwise_op: option A.integer_type) (l: list T.expr) : ML (option string) = match l with
| [] -> None
| a :: q -> Some (mk_args_aux is_bitwise_op (mk_maybe_bitwise_arg is_bitwise_op (mk_expr a)) q)
type reading = { call: string }
type not_reading = { call: string }
type binders = {
is_empty: bool;
bind: string;
args: string;
}
let empty_binders : binders = {
is_empty = true;
bind = "";
args = "";
}
let push_binder (name: string) (typ: string) (b: binders) : binders = {
is_empty = false;
bind = Printf.sprintf "(%s %s) %s" name typ b.bind;
args = Printf.sprintf " %s%s" name b.args;
}
let mk_function_call (name: string) (b: binders) =
Printf.sprintf "%s%s" name b.args
type parser (a: Type) =
(* name *) string ->
(* binders *) binders ->
(* is_toplevel *) bool ->
(* out *) (string -> ML unit) ->
ML a
let unsupported_parser (s: string) (a: Type) : Tot (parser a) =
fun _ _ _ _ -> failwith (Printf.sprintf "unsupported parser: %s" s)
let leaf_reading_parser (name: string) : parser reading =
fun _ _ _ _ -> { call = name }
let readable_itype_parser_suffix (i: I.itype) : Tot string = match i with
| I.UInt8 | I.UInt8BE -> "u8"
| I.UInt16 -> "u16-le"
| I.UInt16BE -> "u16-be"
| I.UInt32 -> "u32-le"
| I.UInt32BE -> "u32-be"
| I.UInt64 -> "u64-le"
| I.UInt64BE -> "u64-be"
| I.Unit -> "empty"
| I.AllBytes -> "all-bytes"
| I.AllZeros -> "all-zeros"
let parse_readable_itype (i: I.readable_itype) : Tot (parser reading) =
leaf_reading_parser ("parse-" ^ readable_itype_parser_suffix i)
let mk_wrap_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(after-state ("^body^" "^input^"))
)
"
let wrap_parser (p: parser reading) : parser not_reading =
fun name binders _ out ->
let name' = Printf.sprintf "%s-wrapped" name in
let body = p name' binders false out in
out (mk_wrap_parser name binders.bind body.call);
{ call = mk_function_call name binders }
let mk_toplevel_parser
(name: string)
(binders: string)
(body: string)
: string
= let input = Printf.sprintf "%s-input" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
("^body^" "^input^")
)
"
let maybe_toplevel_parser (p: parser not_reading) : parser not_reading =
fun name binders is_toplevel out ->
if is_toplevel
then begin
let name' = Printf.sprintf "%s-body" name in
let body = p name' binders false out in
out (mk_toplevel_parser name binders.bind body.call);
{ call = mk_function_call name binders }
end
else p name binders false out
let parse_all_bytes : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-bytes" })
let parse_all_zeros : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-all-zeros" })
let parse_itype : I.itype -> parser not_reading = function
| I.AllBytes -> parse_all_bytes
| I.AllZeros -> parse_all_zeros
| i -> wrap_parser (parse_readable_itype i)
let mk_app_without_paren' id args =
mk_args_aux None id args
let mk_app_without_paren id args =
mk_app_without_paren' (ident_to_string id) args
let parse_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser reading)
= fun _ _ _ _ -> { call = mk_app_without_paren hd args }
let parse_readable_dtyp
(d: I.readable_dtyp)
: Tot (parser reading)
= match d with
| I.DT_IType i -> parse_readable_itype i
| I.DT_App _ hd args -> parse_readable_app hd args
let parse_not_readable_app'
(hd: string)
(args: list I.expr)
: Tot (parser not_reading)
= maybe_toplevel_parser (fun _ _ _ _ -> { call = mk_app_without_paren' hd args })
let parse_not_readable_app
(hd: A.ident)
(args: list I.expr)
: Tot (parser not_reading)
= parse_not_readable_app' (ident_to_string hd) args
let parse_dtyp
(d: I.dtyp)
: Tot (parser not_reading)
= if I.allow_reader_of_dtyp d
then wrap_parser (parse_readable_dtyp d)
else match d with
| I.DT_IType i -> parse_itype i
| I.DT_App _ hd args -> parse_not_readable_app hd args
let parse_false : parser not_reading =
maybe_toplevel_parser (fun _ _ _ _ -> { call = "parse-false" })
let parse_denoted (d: I.dtyp) : parser not_reading =
parse_dtyp d
let mk_parse_pair
(name: string)
(binders: string)
(fst: string)
(snd: string)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^fst^" "^input^")))
(if (< (input-size "^tmp^") 0)
"^tmp^"
("^snd^" "^tmp^")
)
)
)
"
let parse_pair (fst: parser not_reading) (snd: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_fst = Printf.sprintf "%s-fst" name in
let body_fst = fst name_fst binders false out in
let name_snd = Printf.sprintf "%s-snd" name in
let body_snd = snd name_snd binders false out in
out (mk_parse_pair name binders.bind body_fst.call body_snd.call);
{ call = mk_function_call name binders }
let parse_square (p: parser not_reading) : parser not_reading =
fun name binders _ out ->
let body_name = Printf.sprintf "%s-snd" name in
let body = p body_name binders false out in
out (mk_parse_pair name binders.bind body.call body.call);
{ call = mk_function_call name binders }
let mk_parse_dep_pair_with_refinement
(name: string)
(binders: string)
(dfst: string)
(cond_binder_name: string)
(cond: string)
(dsnd_binder_name: string)
(dsnd: string) (* already contains the new argument *)
: string
= let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
let condtmp = Printf.sprintf "%s-condtmp" name in
"(define-fun "^name^" ("^binders^"("^input^" State)) State
(let (("^tmp^" ("^dfst^" "^input^")))
(if (< (input-size (after-state "^tmp^")) 0)
(after-state "^tmp^")
(let (("^condtmp^" (let (("^cond_binder_name^" (return-value "^tmp^"))) "^cond^")))
(if (and "^condtmp^" (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 0)))
(let (("^dsnd_binder_name^" (return-value "^tmp^")))
("^dsnd^"
(mk-state
(input-size (after-state "^tmp^"))
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
(mk-state
(if (and (not "^condtmp^") (or (< (branch-index (after-state "^tmp^")) 0) (= (branch-trace (branch-index (after-state "^tmp^"))) 1))) -1 -2)
(choice-index (after-state "^tmp^"))
(+ (if (< (branch-index (after-state "^tmp^")) 0) 0 1) (branch-index (after-state "^tmp^")))
)
)
)
)
)
)
"
let parse_dep_pair_with_refinement_gen (tag: parser reading) (cond_binder: string) (cond: unit -> ML string) (payload_binder: string) (payload: parser not_reading) : parser not_reading =
fun name binders _ out ->
let name_tag = Printf.sprintf "%s-tag" name in
let body_tag = tag name_tag binders false out in
let binders' = push_binder payload_binder "Int" binders in (* TODO: support more types *)
let name_payload = Printf.sprintf "%s-payload" name in
let body_payload = payload name_payload binders' false out in
out (mk_parse_dep_pair_with_refinement name binders.bind body_tag.call cond_binder (cond ()) payload_binder body_payload.call);
{ call = mk_function_call name binders }
let parse_dep_pair_with_refinement (tag: parser reading) (cond_binder: A.ident) (cond: unit -> ML string) (payload_binder: A.ident) (payload: parser not_reading) : parser not_reading =
parse_dep_pair_with_refinement_gen tag (ident_to_string cond_binder) cond (ident_to_string payload_binder) payload | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
name: Prims.string ->
binders: Prims.string ->
dfst: Prims.string ->
dsnd_binder_name: Prims.string ->
dsnd: Prims.string
-> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.string",
"Prims.op_Hat",
"FStar.Printf.sprintf"
] | [] | false | false | false | true | false | let mk_parse_dep_pair (name binders dfst dsnd_binder_name dsnd: string) : string =
| let input = Printf.sprintf "%s-input" name in
let tmp = Printf.sprintf "%s-tmp" name in
"(define-fun " ^
name ^
" (" ^
binders ^
"(" ^
input ^
" State)) State\n (let ((" ^
tmp ^
" (" ^
dfst ^
" " ^
input ^
")))\n (if (< (input-size (after-state " ^
tmp ^
")) 0)\n (after-state " ^
tmp ^
")\n (let ((" ^
dsnd_binder_name ^
" (return-value " ^
tmp ^ ")))\n (" ^ dsnd ^ " (after-state " ^ tmp ^ "))\n )\n )\n )\n )\n" | false |
Z3TestGen.fst | Z3TestGen.mk_bitwise_not | val mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string | val mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string | let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor "^arg^" #b"^String.make (integer_type_bit_size a) '1'^"))" | {
"file_name": "src/3d/Z3TestGen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 90,
"end_line": 346,
"start_col": 0,
"start_line": 343
} | module Z3TestGen
module Printf = FStar.Printf
open FStar.All
open FStar.Mul
module A = Ast
module T = Target
module I = InterpreterTarget
let prelude : string =
"
(set-option :produce-models true)
(declare-datatypes () ((State (mk-state (input-size Int) (choice-index Int) (branch-index Int)))))
(declare-datatypes () ((Result (mk-result (return-value Int) (after-state State)))))
; From EverParse3d.ErrorCode.is_range_okay
(define-fun is_range_okay ((size Int) (offset Int) (access_size Int)) Bool
(and
(>= size access_size)
(>= (- size access_size) offset)
)
)
(define-fun parse-empty ((x State)) Result
(mk-result 0 x)
)
(declare-fun choose (Int) Int)
(assert (forall ((i Int))
(and (<= 0 (choose i)) (< (choose i) 256))
))
(declare-fun branch-trace (Int) Int)
(define-fun parse-false ((x State)) State
(mk-state -1 (choice-index x) (branch-index x))
)
(define-fun parse-all-bytes ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state 0 (+ (choice-index x) (input-size x)) (branch-index x))
)
)
(define-fun parse-all-zeros ((x State)) State
(if (<= (input-size x) 0)
x
(mk-state
(if
(forall ((j Int))
(if (and (<= 0 j) (< j (input-size x)))
(= (choose (+ (choice-index x) j)) 0)
true
)
)
0
-1
)
(+ (choice-index x) (input-size x))
(branch-index x)
)
)
)
(define-fun parse-u8 ((x State)) Result
(mk-result
(choose (choice-index x))
(mk-state
(let ((new-size (- (input-size x) 1)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 1)
(branch-index x)
)
)
)
(define-fun parse-u16-be ((x State)) Result
(mk-result
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u16-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(choose (+ 1 (choice-index x)))
)
)
(mk-state
(let ((new-size (- (input-size x) 2)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 2)
(branch-index x)
)
)
)
(define-fun parse-u32-be ((x State)) Result
(mk-result
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u32-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(choose (+ 3 (choice-index x)))
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 4)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 4)
(branch-index x)
)
)
)
(define-fun parse-u64-be ((x State)) Result
(mk-result
(+ (choose (+ 7 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(choose (+ 0 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun parse-u64-le ((x State)) Result
(mk-result
(+ (choose (+ 0 (choice-index x)))
(* 256
(+ (choose (+ 1 (choice-index x)))
(* 256
(+ (choose (+ 2 (choice-index x)))
(* 256
(+ (choose (+ 3 (choice-index x)))
(* 256
(+ (choose (+ 4 (choice-index x)))
(* 256
(+ (choose (+ 5 (choice-index x)))
(* 256
(+ (choose (+ 6 (choice-index x)))
(* 256
(choose (+ 7 (choice-index x)))
)
)
)
)
)
)
)
)
)
)
)
)
)
)
(mk-state
(let ((new-size (- (input-size x) 8)))
(if (< new-size 0)
-1
new-size
)
)
(+ (choice-index x) 8)
(branch-index x)
)
)
)
(define-fun-rec pow-2 ((amount Int)) Int
(if (<= amount 0)
1
(* 2 (pow-2 (- amount 1)))
)
)
;; see LowParse.BitFields.get_bitfield_eq
(define-fun get-bitfield-lsb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(mod (div value (pow-2 bitsFrom)) (pow-2 (- bitsTo bitsFrom)))
)
;; see EverParse3d.Prelude.StaticHeader
(define-fun get-bitfield-msb ((nbBits Int) (value Int) (bitsFrom Int) (bitsTo Int)) Int
(get-bitfield-lsb nbBits value (- nbBits bitsTo) (- nbBits bitsFrom))
)
;; see EverParse3d.Actions.Base.validate_nlist_total_constant_size
(define-fun parse-nlist-total-constant-size ((size Int) (eltSize Int) (x State)) State
(if (< (input-size x) 0)
x
(if (and (= 0 (mod size eltSize)) (>= (input-size x) size))
(mk-state
(- (input-size x) size)
(+ (choice-index x) size)
(branch-index x)
)
(mk-state
-1
(choice-index x)
(branch-index x)
)
)
)
)
(declare-const initial-input-size Int)
(assert (>= initial-input-size 0))
(define-fun initial-state () State (mk-state initial-input-size 0 0))
"
let mk_constant = function
| A.Unit -> "0"
| A.Int _ x -> string_of_int x
| A.XInt _ x -> string_of_int (OS.int_of_string x)
| A.Bool true -> "true"
| A.Bool false -> "false"
let mk_app fn = function
| None -> fn
| Some args -> Printf.sprintf "(%s %s)" fn args
let assert_some = function
| None -> failwith "assert_some"
| Some x -> x
let is_bitwise_op (x: T.op) : Tot (option A.integer_type) =
match x with
| T.BitwiseAnd a
| T.BitwiseXor a
| T.BitwiseOr a
| T.BitwiseNot a
| T.ShiftLeft a
| T.ShiftRight a
-> Some a
| _ -> None
let mk_bitwise_op (op: string) (bitvec_args: option string) : ML string =
mk_app "bv2int" (Some (mk_app op bitvec_args))
let integer_type_bit_size = function
| A.UInt8 -> 8
| A.UInt16 -> 16
| A.UInt32 -> 32
| A.UInt64 -> 64 | {
"checked_file": "/",
"dependencies": [
"Z3.fsti.checked",
"Target.fsti.checked",
"prims.fst.checked",
"OS.fsti.checked",
"Lisp.fsti.checked",
"InterpreterTarget.fsti.checked",
"FStar.String.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.List.fst.checked",
"FStar.IO.fst.checked",
"FStar.Char.fsti.checked",
"FStar.All.fst.checked",
"Ast.fst.checked"
],
"interface_file": false,
"source_file": "Z3TestGen.fst"
} | [
{
"abbrev": true,
"full_module": "InterpreterTarget",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "Target",
"short_module": "T"
},
{
"abbrev": true,
"full_module": "Ast",
"short_module": "A"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.All",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Printf",
"short_module": "Printf"
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Ast.integer_type -> bitvec_arg: FStar.Pervasives.Native.option Prims.string
-> FStar.All.ML Prims.string | FStar.All.ML | [
"ml"
] | [] | [
"Ast.integer_type",
"FStar.Pervasives.Native.option",
"Prims.string",
"FStar.All.failwith",
"Prims.op_Hat",
"FStar.String.make",
"Z3TestGen.integer_type_bit_size"
] | [] | false | true | false | false | false | let mk_bitwise_not (a: A.integer_type) (bitvec_arg: option string) : ML string =
| match bitvec_arg with
| None -> failwith "ill-formed bitwise_not"
| Some arg -> "(bv2int (bvxor " ^ arg ^ " #b" ^ String.make (integer_type_bit_size a) '1' ^ "))" | false |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.