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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LowParse.BitFields.fst | LowParse.BitFields.nth_pow2_minus_one | val nth_pow2_minus_one (#n: pos) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires True) (ensures (pow2 m <= pow2 n /\ (nth #n (pow2 m - 1) i == (i < m)))) | val nth_pow2_minus_one (#n: pos) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires True) (ensures (pow2 m <= pow2 n /\ (nth #n (pow2 m - 1) i == (i < m)))) | let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 78,
"start_col": 0,
"start_line": 71
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | m: Prims.nat{m <= n} -> i: Prims.nat{i < n}
-> FStar.Pervasives.Lemma
(ensures Prims.pow2 m <= Prims.pow2 n /\ LowParse.BitFields.nth (Prims.pow2 m - 1) i == (i < m)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.BitFields.nth_pow2_minus_one'",
"Prims.op_Subtraction",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Prims.pow2",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let nth_pow2_minus_one (#n: pos) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires True) (ensures (pow2 m <= pow2 n /\ (nth #n (pow2 m - 1) i == (i < m)))) =
| nth_pow2_minus_one' #n m (n - 1 - i) | false |
LowParse.BitFields.fst | LowParse.BitFields.nth | val nth (#n: pos) (a: U.uint_t n) (i: nat{i < n}) : Tot bool | val nth (#n: pos) (a: U.uint_t n) (i: nat{i < n}) : Tot bool | let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 21,
"end_line": 49,
"start_col": 0,
"start_line": 48
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t n -> i: Prims.nat{i < n} -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt.nth",
"Prims.op_Subtraction",
"Prims.bool"
] | [] | false | false | false | false | false | let nth (#n: pos) (a: U.uint_t n) (i: nat{i < n}) : Tot bool =
| U.nth a (n - 1 - i) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask | val bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) | val bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) | let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 49,
"end_line": 13,
"start_col": 0,
"start_line": 8
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot} -> FStar.UInt.uint_t tot | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Pervasives.normalize_term",
"FStar.UInt.uint_t",
"FStar.Mul.op_Star",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"FStar.Math.Lemmas.pow2_plus",
"FStar.Math.Lemmas.pow2_le_compat"
] | [] | false | false | false | false | false | let bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
| [@@ inline_let ]let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.eq_nth | val eq_nth (#n: pos) (a b: U.uint_t n) (f: (i: nat{i < n} -> Lemma (nth a i == nth b i)))
: Lemma (a == b) | val eq_nth (#n: pos) (a b: U.uint_t n) (f: (i: nat{i < n} -> Lemma (nth a i == nth b i)))
: Lemma (a == b) | let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 17,
"end_line": 69,
"start_col": 0,
"start_line": 51
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t n ->
b: FStar.UInt.uint_t n ->
f:
(i: Prims.nat{i < n}
-> FStar.Pervasives.Lemma
(ensures LowParse.BitFields.nth a i == LowParse.BitFields.nth b i))
-> FStar.Pervasives.Lemma (ensures a == b) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"Prims.Nil",
"FStar.Pervasives.pattern",
"FStar.UInt.nth_lemma",
"FStar.Classical.forall_intro",
"FStar.UInt.nth",
"Prims.op_Subtraction"
] | [] | false | false | true | false | false | let eq_nth (#n: pos) (a b: U.uint_t n) (f: (i: nat{i < n} -> Lemma (nth a i == nth b i)))
: Lemma (a == b) =
| let g (i: nat{i < n}) : Lemma (U.nth a i == U.nth b i) = f (n - 1 - i) in
Classical.forall_intro g;
U.nth_lemma a b | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_bitfield_mask | val nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi)) | val nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi)) | let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 7,
"end_line": 106,
"start_col": 0,
"start_line": 98
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot} -> i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.nth (LowParse.BitFields.bitfield_mask tot lo hi) i == (lo <= i && i < hi)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"Prims.bool",
"LowParse.BitFields.nth_pow2_minus_one",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.nth_shift_left",
"Prims.pow2",
"LowParse.BitFields.bitfield_mask_eq",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"LowParse.BitFields.nth",
"LowParse.BitFields.bitfield_mask",
"Prims.op_AmpAmp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi)) =
| bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo then () else nth_pow2_minus_one #tot (hi - lo) (i - lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_raw | val get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (U.uint_t tot) | val get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (U.uint_t tot) | let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 59,
"end_line": 109,
"start_col": 0,
"start_line": 108
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.UInt.uint_t tot | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt.shift_right",
"FStar.UInt.logand",
"LowParse.BitFields.bitfield_mask"
] | [] | false | false | false | false | false | let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (U.uint_t tot) =
| (x `U.logand` (bitfield_mask tot lo hi)) `U.shift_right` lo | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_pow2_minus_one' | val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true))) | val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true))) | let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 46,
"end_line": 44,
"start_col": 0,
"start_line": 38
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\ | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | m: Prims.nat{m <= n} -> i: Prims.nat{i < n}
-> FStar.Pervasives.Lemma
(ensures
Prims.pow2 m <= Prims.pow2 n /\ (i < n - m ==> FStar.UInt.nth (Prims.pow2 m - 1) i == false) /\
(n - m <= i ==> FStar.UInt.nth (Prims.pow2 m - 1) i == true)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt.one_to_vec_lemma",
"Prims.bool",
"Prims.l_or",
"Prims.l_and",
"Prims.op_GreaterThanOrEqual",
"Prims.op_GreaterThan",
"FStar.UInt.ones_to_vec_lemma",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_pow2_minus_one'",
"Prims.unit",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.pow2"
] | [
"recursion"
] | false | false | true | false | false | let rec nth_pow2_minus_one' #n m i =
| let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0
then U.one_to_vec_lemma #n i
else
if m = n
then U.ones_to_vec_lemma #n i
else if i = n - 1 then () else nth_pow2_minus_one' #(n - 1) (m - 1) i | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_get_bitfield | val nth_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo))) | val nth_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo))) | let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 32,
"end_line": 210,
"start_col": 0,
"start_line": 208
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.nth (LowParse.BitFields.get_bitfield x lo hi) i ==
(i < hi - lo && LowParse.BitFields.nth x (i + lo))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.BitFields.nth_get_bitfield_raw",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_AmpAmp",
"Prims.op_Subtraction",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let nth_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo))) =
| nth_get_bitfield_raw x lo hi i | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_le_pow2_m | val nth_le_pow2_m (#n: pos) (a: U.uint_t n) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires (a < pow2 m /\ m <= i)) (ensures (nth a i == false)) | val nth_le_pow2_m (#n: pos) (a: U.uint_t n) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires (a < pow2 m /\ m <= i)) (ensures (nth a i == false)) | let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 29,
"end_line": 192,
"start_col": 0,
"start_line": 181
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t n -> m: Prims.nat{m <= n} -> i: Prims.nat{i < n}
-> FStar.Pervasives.Lemma (requires a < Prims.pow2 m /\ m <= i)
(ensures LowParse.BitFields.nth a i == false) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.BitFields.nth_logand",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"LowParse.BitFields.nth_pow2_minus_one",
"FStar.Math.Lemmas.modulo_lemma",
"LowParse.BitFields.logand_mask",
"Prims.l_and",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let nth_le_pow2_m (#n: pos) (a: U.uint_t n) (m: nat{m <= n}) (i: nat{i < n})
: Lemma (requires (a < pow2 m /\ m <= i)) (ensures (nth a i == false)) =
| logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_get_bitfield_raw | val nth_get_bitfield_raw
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo))) | val nth_get_bitfield_raw
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo))) | let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
() | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 6,
"end_line": 143,
"start_col": 0,
"start_line": 135
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.nth (LowParse.BitFields.get_bitfield_raw x lo hi) i ==
(i < hi - lo && LowParse.BitFields.nth x (i + lo))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"Prims.op_Addition",
"LowParse.BitFields.nth_bitfield_mask",
"Prims.unit",
"LowParse.BitFields.nth_logand",
"LowParse.BitFields.bitfield_mask",
"Prims.bool",
"LowParse.BitFields.nth_shift_right",
"FStar.UInt.logand",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"LowParse.BitFields.nth",
"LowParse.BitFields.get_bitfield_raw",
"Prims.op_AmpAmp",
"Prims.op_Subtraction",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let nth_get_bitfield_raw
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(i: nat{i < tot})
: Lemma (nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo))) =
| nth_shift_right (x `U.logand` (bitfield_mask tot lo hi)) lo i;
if i + lo < tot
then
(nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_not_bitfield_mask | val nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i)) | val nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i)) | let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 31,
"end_line": 295,
"start_col": 0,
"start_line": 292
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot} -> i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.nth (LowParse.BitFields.not_bitfield_mask tot lo hi) i ==
(i < lo || hi <= i)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.BitFields.nth_bitfield_mask",
"Prims.unit",
"LowParse.BitFields.nth_lognot",
"LowParse.BitFields.bitfield_mask",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"LowParse.BitFields.not_bitfield_mask",
"Prims.op_BarBar",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) (i: nat{i < tot})
: Lemma (nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i)) =
| nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_raw_bounded | val get_bitfield_raw_bounded
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield_raw x lo hi < pow2 (hi - lo)) | val get_bitfield_raw_bounded
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield_raw x lo hi < pow2 (hi - lo)) | let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo)) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 200,
"start_col": 0,
"start_line": 194
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures LowParse.BitFields.get_bitfield_raw x lo hi < Prims.pow2 (hi - lo)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.lemma_mod_lt",
"Prims.pow2",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.logand_mask",
"LowParse.BitFields.get_bitfield_raw",
"LowParse.BitFields.get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one",
"Prims.l_True",
"Prims.squash",
"Prims.op_LessThan",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let get_bitfield_raw_bounded
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield_raw x lo hi < pow2 (hi - lo)) =
| get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask_mod_pow2_lo | val bitfield_mask_mod_pow2_lo
(tot: pos)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(m: nat{m <= lo})
: Lemma (bitfield_mask tot lo hi % pow2 m == 0) | val bitfield_mask_mod_pow2_lo
(tot: pos)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(m: nat{m <= lo})
: Lemma (bitfield_mask tot lo hi % pow2 m == 0) | let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 64,
"end_line": 262,
"start_col": 0,
"start_line": 258
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot} -> m: Prims.nat{m <= lo}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.bitfield_mask tot lo hi % Prims.pow2 m == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.pow2_multiplication_modulo_lemma_1",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"LowParse.BitFields.bitfield_mask",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let bitfield_mask_mod_pow2_lo
(tot: pos)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(m: nat{m <= lo})
: Lemma (bitfield_mask tot lo hi % pow2 m == 0) =
| M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield | val set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot) | val set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot) | let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 76,
"end_line": 301,
"start_col": 0,
"start_line": 297
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo)
-> FStar.UInt.uint_t tot | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"FStar.UInt.logor",
"FStar.UInt.logand",
"LowParse.BitFields.not_bitfield_mask",
"FStar.UInt.shift_left"
] | [] | false | false | false | false | false | let set_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot) =
| (x `U.logand` (not_bitfield_mask tot lo hi)) `U.logor` (v `U.shift_left` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield | val get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo)) | val get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo)) | let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 26,
"end_line": 206,
"start_col": 0,
"start_line": 202
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> LowParse.BitFields.ubitfield tot (hi - lo) | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.get_bitfield_raw",
"Prims.unit",
"LowParse.BitFields.get_bitfield_raw_bounded",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction"
] | [] | false | false | false | false | false | let get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo)) =
| get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one | val get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\ y == y `U.logand` (pow2 (hi - lo) - 1)) | val get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\ y == y `U.logand` (pow2 (hi - lo) - 1)) | let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 156,
"start_col": 0,
"start_line": 145
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
(let y = LowParse.BitFields.get_bitfield_raw x lo hi in
Prims.pow2 (hi - lo) - 1 < Prims.pow2 tot /\
y == FStar.UInt.logand y (Prims.pow2 (hi - lo) - 1))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"FStar.UInt.logand",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_LessThan",
"LowParse.BitFields.nth_logand",
"Prims.unit",
"LowParse.BitFields.nth_pow2_minus_one",
"LowParse.BitFields.nth_get_bitfield_raw",
"LowParse.BitFields.get_bitfield_raw",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\ y == y `U.logand` (pow2 (hi - lo) - 1)) =
| nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y
(y `U.logand` (pow2 (hi - lo) - 1))
(fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask_lt_pow2_hi | val bitfield_mask_lt_pow2_hi (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (bitfield_mask tot lo hi < pow2 hi) | val bitfield_mask_lt_pow2_hi (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (bitfield_mask tot lo hi < pow2 hi) | let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 28,
"end_line": 270,
"start_col": 0,
"start_line": 264
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.bitfield_mask tot lo hi < Prims.pow2 hi) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.pow2_plus",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"Prims.op_LessThan",
"LowParse.BitFields.bitfield_mask",
"Prims.pow2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let bitfield_mask_lt_pow2_hi (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (bitfield_mask tot lo hi < pow2 hi) =
| M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo | false |
LowParse.BitFields.fst | LowParse.BitFields.not_bitfield_mask | val not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)}) | val not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)}) | let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 24,
"end_line": 290,
"start_col": 0,
"start_line": 273
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> x: FStar.UInt.uint_t tot {x == FStar.UInt.lognot (LowParse.BitFields.bitfield_mask tot lo hi)} | Prims.Tot | [
"total"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Pervasives.normalize_term",
"FStar.UInt.uint_t",
"Prims.eq2",
"FStar.UInt.lognot",
"LowParse.BitFields.bitfield_mask",
"Prims.op_Addition",
"Prims.unit",
"LowParse.BitFields.eq_nth",
"FStar.UInt.logor",
"Prims.op_LessThan",
"LowParse.BitFields.nth_bitfield_mask",
"LowParse.BitFields.logor_disjoint",
"LowParse.BitFields.bitfield_mask_lt_pow2_hi",
"LowParse.BitFields.bitfield_mask_mod_pow2_lo"
] | [] | false | false | false | false | false | let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)}) =
| [@@ inline_let ]let a = bitfield_mask tot hi tot in
[@@ inline_let ]let b = bitfield_mask tot 0 lo in
[@@ inline_let ]let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b)
(U.lognot (bitfield_mask tot lo hi))
(fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i)
in
normalize_term (a + b) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_logor | val get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi) | val get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi) | let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 220,
"start_col": 0,
"start_line": 212
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
y: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield (FStar.UInt.logor x y) lo hi ==
FStar.UInt.logor (LowParse.BitFields.get_bitfield x lo hi)
(LowParse.BitFields.get_bitfield y lo hi)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"FStar.UInt.logor",
"Prims.op_LessThan",
"LowParse.BitFields.nth_get_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_logor (#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == (get_bitfield x lo hi) `U.logor` (get_bitfield y lo hi)) =
| eq_nth (get_bitfield (x `U.logor` y) lo hi)
((get_bitfield x lo hi) `U.logor` (get_bitfield y lo hi))
(fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_zero | val get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0) | val get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0) | let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 422,
"start_col": 0,
"start_line": 412
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.get_bitfield 0 lo hi == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_zero",
"Prims.op_Addition",
"Prims.bool",
"Prims.unit",
"LowParse.BitFields.nth_get_bitfield",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_zero (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield #tot 0 lo hi == 0) =
| eq_nth (get_bitfield #tot 0 lo hi)
0
(fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo then nth_zero tot (i + lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_set_bitfield | val nth_set_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(i: nat{i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i)) | val nth_set_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(i: nat{i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i)) | let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i)) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 71,
"end_line": 328,
"start_col": 0,
"start_line": 303
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo) ->
i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.nth (LowParse.BitFields.set_bitfield x lo hi v) i ==
(match lo <= i && i < hi with
| true -> LowParse.BitFields.nth v (i - lo)
| _ -> LowParse.BitFields.nth x i)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"Prims.op_LessThan",
"Prims._assert",
"Prims.eq2",
"Prims.bool",
"Prims.op_AmpAmp",
"LowParse.BitFields.nth",
"Prims.unit",
"LowParse.BitFields.nth_le_pow2_m",
"Prims.op_BarBar",
"LowParse.BitFields.nth_shift_left",
"FStar.UInt.shift_left",
"LowParse.BitFields.nth_not_bitfield_mask",
"LowParse.BitFields.not_bitfield_mask",
"LowParse.BitFields.nth_logand",
"FStar.UInt.logand",
"LowParse.BitFields.nth_logor",
"LowParse.BitFields.set_bitfield",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let nth_set_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(i: nat{i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i)) =
| let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` (not_bitfield_mask tot lo hi)) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` (not_bitfield_mask tot lo hi)) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi)
then assert (y == nth v (i - lo))
else
if (i < hi)
then assert (y == nth x i)
else
(nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i));
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i)) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_set_bitfield_same_gen | val set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v')) | val set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v')) | let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 373,
"start_col": 0,
"start_line": 362
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo) ->
lo': Prims.nat ->
hi': Prims.nat{lo' <= lo /\ hi <= hi' /\ hi' <= tot} ->
v': LowParse.BitFields.ubitfield tot (hi' - lo')
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.set_bitfield (LowParse.BitFields.set_bitfield x lo hi v) lo' hi' v' ==
LowParse.BitFields.set_bitfield x lo' hi' v') | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"Prims.op_LessThan",
"LowParse.BitFields.nth_set_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_set_bitfield_same_gen
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(lo': nat)
(hi': nat{lo' <= lo /\ hi <= hi' /\ hi' <= tot})
(v': ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v')) =
| eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v')
(set_bitfield x lo' hi' v')
(fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_full | val get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x) | val get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x) | let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 431,
"start_col": 0,
"start_line": 424
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.get_bitfield x 0 tot == x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"LowParse.BitFields.nth_get_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_full (#tot: pos) (x: U.uint_t tot) : Lemma (get_bitfield x 0 tot == x) =
| eq_nth (get_bitfield x 0 tot) x (fun i -> nth_get_bitfield x 0 tot i) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_logxor | val get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) | val get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) | let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 230,
"start_col": 0,
"start_line": 222
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
y: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield (FStar.UInt.logxor x y) lo hi ==
FStar.UInt.logxor (LowParse.BitFields.get_bitfield x lo hi)
(LowParse.BitFields.get_bitfield y lo hi)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"FStar.UInt.logxor",
"Prims.op_LessThan",
"LowParse.BitFields.nth_get_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_logxor (#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == (get_bitfield x lo hi) `U.logxor` (get_bitfield y lo hi)
) =
| eq_nth (get_bitfield (x `U.logxor` y) lo hi)
((get_bitfield x lo hi) `U.logxor` (get_bitfield y lo hi))
(fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i) | false |
LowParse.BitFields.fst | LowParse.BitFields.logor_disjoint | val logor_disjoint (#n: pos) (a b: U.uint_t n) (m: nat{m <= n})
: Lemma (requires (a % pow2 m == 0 /\ b < pow2 m)) (ensures (U.logor #n a b == a + b)) | val logor_disjoint (#n: pos) (a b: U.uint_t n) (m: nat{m <= n})
: Lemma (requires (a % pow2 m == 0 /\ b < pow2 m)) (ensures (U.logor #n a b == a + b)) | let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 26,
"end_line": 254,
"start_col": 0,
"start_line": 234
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt.uint_t n -> b: FStar.UInt.uint_t n -> m: Prims.nat{m <= n}
-> FStar.Pervasives.Lemma (requires a % Prims.pow2 m == 0 /\ b < Prims.pow2 m)
(ensures FStar.UInt.logor a b == a + b) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt.logor_lemma_1",
"Prims.bool",
"Prims.l_or",
"Prims.op_GreaterThan",
"Prims.l_and",
"Prims.op_GreaterThanOrEqual",
"Prims.unit",
"FStar.UInt.logor_commutative",
"FStar.Math.Lemmas.modulo_lemma",
"Prims.pow2",
"FStar.UInt.logor_disjoint",
"Prims.eq2",
"Prims.op_Modulus",
"Prims.op_LessThan",
"Prims.squash",
"FStar.UInt.logor",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let logor_disjoint (#n: pos) (a b: U.uint_t n) (m: nat{m <= n})
: Lemma (requires (a % pow2 m == 0 /\ b < pow2 m)) (ensures (U.logor #n a b == a + b)) =
| if m = 0
then U.logor_lemma_1 a
else
if m = n
then
(M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b)
else U.logor_disjoint a b m | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_set_bitfield_other | val get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi')) | val get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi')) | let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 360,
"start_col": 0,
"start_line": 346
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo) ->
lo': Prims.nat ->
hi': Prims.nat{lo' <= hi' /\ hi' <= tot}
-> FStar.Pervasives.Lemma (requires hi' <= lo \/ hi <= lo')
(ensures
LowParse.BitFields.get_bitfield (LowParse.BitFields.set_bitfield x lo hi v) lo' hi' ==
LowParse.BitFields.get_bitfield x lo' hi') | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"LowParse.BitFields.set_bitfield",
"Prims.op_LessThan",
"LowParse.BitFields.nth_set_bitfield",
"Prims.op_Addition",
"Prims.bool",
"Prims.unit",
"LowParse.BitFields.nth_get_bitfield",
"Prims.l_or",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_set_bitfield_other
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(lo': nat)
(hi': nat{lo' <= hi' /\ hi' <= tot})
: Lemma (requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi')) =
| eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi')
(get_bitfield x lo' hi')
(fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo' then nth_set_bitfield x lo hi v (i + lo')) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_empty | val set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x) | val set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x) | let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 403,
"start_col": 0,
"start_line": 397
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> i: Prims.nat{i <= tot} -> y: LowParse.BitFields.ubitfield tot 0
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.set_bitfield x i i y == x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"Prims.op_LessThan",
"LowParse.BitFields.nth_set_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_empty (#tot: pos) (x: U.uint_t tot) (i: nat{i <= tot}) (y: ubitfield tot 0)
: Lemma (set_bitfield x i i y == x) =
| eq_nth (set_bitfield x i i y) x (fun j -> nth_set_bitfield x i i y j) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_set_bitfield_other | val set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v)) | val set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v)) | let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 387,
"start_col": 0,
"start_line": 375
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo) ->
lo': Prims.nat ->
hi': Prims.nat{lo' <= hi' /\ hi' <= tot} ->
v': LowParse.BitFields.ubitfield tot (hi' - lo')
-> FStar.Pervasives.Lemma (requires hi' <= lo \/ hi <= lo')
(ensures
LowParse.BitFields.set_bitfield (LowParse.BitFields.set_bitfield x lo hi v) lo' hi' v' ==
LowParse.BitFields.set_bitfield (LowParse.BitFields.set_bitfield x lo' hi' v') lo hi v) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"Prims.op_LessThan",
"LowParse.BitFields.nth_set_bitfield",
"Prims.unit",
"Prims.l_or",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_set_bitfield_other
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
(lo': nat)
(hi': nat{lo' <= hi' /\ hi' <= tot})
(v': ubitfield tot (hi' - lo'))
: Lemma (requires (hi' <= lo \/ hi <= lo'))
(ensures
(set_bitfield (set_bitfield x lo hi v) lo' hi' v' ==
set_bitfield (set_bitfield x lo' hi' v') lo hi v)) =
| eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v')
(set_bitfield (set_bitfield x lo' hi' v') lo hi v)
(fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i) | false |
LowParse.BitFields.fst | LowParse.BitFields.logand_mask | val logand_mask (#n: pos) (a: U.uint_t n) (m: nat{m <= n})
: Lemma (pow2 m <= pow2 n /\ U.logand a (pow2 m - 1) == a % pow2 m) | val logand_mask (#n: pos) (a: U.uint_t n) (m: nat{m <= n})
: Lemma (pow2 m <= pow2 n /\ U.logand a (pow2 m - 1) == a % pow2 m) | let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 177,
"start_col": 0,
"start_line": 160
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt.uint_t n -> m: Prims.nat{m <= n}
-> FStar.Pervasives.Lemma
(ensures
Prims.pow2 m <= Prims.pow2 n /\ FStar.UInt.logand a (Prims.pow2 m - 1) == a % Prims.pow2 m) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt.logand_lemma_1",
"Prims.bool",
"Prims.l_or",
"Prims.op_GreaterThan",
"Prims.l_and",
"Prims.op_GreaterThanOrEqual",
"FStar.Math.Lemmas.modulo_lemma",
"Prims.pow2",
"Prims.unit",
"FStar.UInt.logand_lemma_2",
"FStar.UInt.logand_mask",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.UInt.logand",
"Prims.op_Subtraction",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let logand_mask (#n: pos) (a: U.uint_t n) (m: nat{m <= n})
: Lemma (pow2 m <= pow2 n /\ U.logand a (pow2 m - 1) == a % pow2 m) =
| M.pow2_le_compat n m;
if m = 0
then U.logand_lemma_1 a
else
if m = n
then
(U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m))
else U.logand_mask a m | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_zero | val nth_zero (tot: pos) (i: nat{i < tot}) : Lemma (nth #tot 0 i == false) | val nth_zero (tot: pos) (i: nat{i < tot}) : Lemma (nth #tot 0 i == false) | let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 25,
"end_line": 410,
"start_col": 0,
"start_line": 405
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.nth 0 i == false) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt.zero_nth_lemma",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.bool",
"LowParse.BitFields.nth",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let nth_zero (tot: pos) (i: nat{i < tot}) : Lemma (nth #tot 0 i == false) =
| U.zero_nth_lemma #tot i | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_full | val set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y) | val set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y) | let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 395,
"start_col": 0,
"start_line": 389
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> y: LowParse.BitFields.ubitfield tot tot
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.set_bitfield x 0 tot y == y) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"LowParse.BitFields.ubitfield",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"LowParse.BitFields.nth_set_bitfield",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_full (#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma (set_bitfield x 0 tot y == y) =
| eq_nth (set_bitfield x 0 tot y) y (fun i -> nth_set_bitfield x 0 tot y i) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_empty | val get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0) | val get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0) | let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 442,
"start_col": 0,
"start_line": 433
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> i: Prims.nat{i <= tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.get_bitfield x i i == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_LessThan",
"LowParse.BitFields.nth_zero",
"Prims.unit",
"LowParse.BitFields.nth_get_bitfield",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_empty (#tot: pos) (x: U.uint_t tot) (i: nat{i <= tot})
: Lemma (get_bitfield x i i == 0) =
| eq_nth (get_bitfield x i i)
0
(fun j ->
nth_get_bitfield x i i j;
nth_zero tot j) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_partition | val get_bitfield_partition
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi)) | val get_bitfield_partition
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi)) | let get_bitfield_partition = get_bitfield_partition' | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 52,
"end_line": 696,
"start_col": 0,
"start_line": 696
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
y: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
l: Prims.list Prims.nat
-> FStar.Pervasives.Lemma (requires LowParse.BitFields.get_bitfield_partition_prop x y lo hi l)
(ensures LowParse.BitFields.get_bitfield x lo hi == LowParse.BitFields.get_bitfield y lo hi) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"LowParse.BitFields.get_bitfield_partition'"
] | [] | true | false | true | false | false | let get_bitfield_partition =
| get_bitfield_partition' | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_partition_2 | val get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
)) | val get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
)) | let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 21,
"end_line": 678,
"start_col": 0,
"start_line": 664
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | mid: Prims.nat{mid <= tot} -> x: FStar.UInt.uint_t tot -> y: FStar.UInt.uint_t tot
-> FStar.Pervasives.Lemma
(requires
LowParse.BitFields.get_bitfield x 0 mid == LowParse.BitFields.get_bitfield y 0 mid /\
LowParse.BitFields.get_bitfield x mid tot == LowParse.BitFields.get_bitfield y mid tot)
(ensures x == y) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt.uint_t",
"LowParse.BitFields.get_bitfield_full",
"Prims.unit",
"LowParse.BitFields.get_bitfield_partition_2_gen",
"Prims.l_and",
"Prims.eq2",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.get_bitfield",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let get_bitfield_partition_2 (#tot: pos) (mid: nat{mid <= tot}) (x y: U.uint_t tot)
: Lemma
(requires
(get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot)) (ensures (x == y)) =
| get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_partition' | val get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(l: list nat)
: Lemma (requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l) | val get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(l: list nat)
: Lemma (requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l) | let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 45,
"end_line": 694,
"start_col": 0,
"start_line": 680
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
y: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
l: Prims.list Prims.nat
-> FStar.Pervasives.Lemma (requires LowParse.BitFields.get_bitfield_partition_prop x y lo hi l)
(ensures LowParse.BitFields.get_bitfield x lo hi == LowParse.BitFields.get_bitfield y lo hi)
(decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.list",
"LowParse.BitFields.get_bitfield_partition_2_gen",
"Prims.unit",
"LowParse.BitFields.get_bitfield_partition'",
"LowParse.BitFields.get_bitfield_partition_prop",
"Prims.squash",
"Prims.eq2",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.get_bitfield",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(l: list nat)
: Lemma (requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l) =
| match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y | false |
LowParse.BitFields.fst | LowParse.BitFields.pow2_m_minus_one_eq | val pow2_m_minus_one_eq (n m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) | val pow2_m_minus_one_eq (n m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) | let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 64,
"end_line": 844,
"start_col": 0,
"start_line": 834
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | n: Prims.nat -> m: Prims.nat
-> FStar.Pervasives.Lemma (requires m <= n)
(ensures (Prims.pow2 n - 1) / Prims.pow2 m == Prims.pow2 (n - m) - 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"FStar.Math.Lemmas.division_definition",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"FStar.Math.Lemmas.pow2_plus",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Division",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let pow2_m_minus_one_eq (n m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) =
| M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq64_lhs | val bitfield_eq64_lhs (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64}) : Tot U64.t | val bitfield_eq64_lhs (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64}) : Tot U64.t | let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 927,
"start_col": 0,
"start_line": 924
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 64} -> FStar.UInt64.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.logand",
"LowParse.BitFields.bitfield_mask64"
] | [] | false | false | false | false | false | let bitfield_eq64_lhs (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64}) : Tot U64.t =
| x `U64.logand` (bitfield_mask64 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.nth_size | val nth_size (n1: nat) (n2: nat{n1 <= n2}) (x: U.uint_t n1) (i: nat{i < n2})
: Lemma (x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i)) | val nth_size (n1: nat) (n2: nat{n1 <= n2}) (x: U.uint_t n1) (i: nat{i < n2})
: Lemma (x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i)) | let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 706,
"start_col": 0,
"start_line": 698
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition' | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | n1: Prims.nat -> n2: Prims.nat{n1 <= n2} -> x: FStar.UInt.uint_t n1 -> i: Prims.nat{i < n2}
-> FStar.Pervasives.Lemma
(ensures
x < Prims.pow2 n2 /\ LowParse.BitFields.nth x i == (i < n1 && LowParse.BitFields.nth x i)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt.uint_t",
"Prims.op_LessThan",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"LowParse.BitFields.nth_size",
"Prims.op_Subtraction",
"Prims.op_Division",
"Prims.unit",
"LowParse.BitFields.nth_le_pow2_m",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Prims.pow2",
"Prims.eq2",
"LowParse.BitFields.nth",
"Prims.op_AmpAmp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec nth_size (n1: nat) (n2: nat{n1 <= n2}) (x: U.uint_t n1) (i: nat{i < n2})
: Lemma (x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i)) =
| M.pow2_le_compat n2 n1;
if i < n1
then if i = 0 then () else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
else nth_le_pow2_m #n2 x n1 i | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_size | val get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat)) | val get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat)) | let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 730,
"start_col": 0,
"start_line": 708
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 |
tot1: Prims.pos ->
tot2: Prims.pos ->
x: Prims.nat{x < Prims.pow2 tot1 /\ tot1 <= tot2} ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot1}
-> FStar.Pervasives.Lemma
(ensures
x < Prims.pow2 tot2 /\
LowParse.BitFields.get_bitfield x lo hi == LowParse.BitFields.get_bitfield x lo hi) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.pow2",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_size",
"Prims.op_Addition",
"Prims.bool",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.op_AmpAmp",
"LowParse.BitFields.nth",
"LowParse.BitFields.nth_get_bitfield",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_size
(tot1 tot2: pos)
(x: nat{x < pow2 tot1 /\ tot1 <= tot2})
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot1})
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat)) =
| M.pow2_le_compat tot2 tot1;
eq_nth #tot2
(get_bitfield #tot1 x lo hi)
(get_bitfield #tot2 x lo hi)
(fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then
(nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo then nth_size tot1 tot2 x (i + lo))) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq32_lhs | val bitfield_eq32_lhs (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32}) : Tot U32.t | val bitfield_eq32_lhs (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32}) : Tot U32.t | let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 1004,
"start_col": 0,
"start_line": 1001
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 32} -> FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.logand",
"LowParse.BitFields.bitfield_mask32"
] | [] | false | false | false | false | false | let bitfield_eq32_lhs (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32}) : Tot U32.t =
| x `U32.logand` (bitfield_mask32 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_size | val set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat)) | val set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat)) | let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 753,
"start_col": 0,
"start_line": 732
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 |
tot1: Prims.pos ->
tot2: Prims.pos ->
x: Prims.nat{x < Prims.pow2 tot1 /\ tot1 <= tot2} ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot1} ->
v: LowParse.BitFields.ubitfield tot1 (hi - lo)
-> FStar.Pervasives.Lemma
(ensures
x < Prims.pow2 tot2 /\ v < Prims.pow2 tot2 /\
LowParse.BitFields.set_bitfield x lo hi v == LowParse.BitFields.set_bitfield x lo hi v) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.pow2",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"Prims.op_AmpAmp",
"LowParse.BitFields.nth_size",
"Prims.bool",
"Prims.unit",
"LowParse.BitFields.nth_set_bitfield",
"LowParse.BitFields.nth",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_size
(tot1 tot2: pos)
(x: nat{x < pow2 tot1 /\ tot1 <= tot2})
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot1})
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\
(set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat)) =
| M.pow2_le_compat tot2 tot1;
eq_nth #tot2
(set_bitfield #tot1 x lo hi v)
(set_bitfield #tot2 x lo hi v)
(fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then
(nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi then nth_size tot1 tot2 v (i - lo))) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_eq | val get_bitfield_eq
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo)) | val get_bitfield_eq
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo)) | let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 832,
"start_col": 0,
"start_line": 809
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures LowParse.BitFields.get_bitfield x lo hi == x / Prims.pow2 lo % Prims.pow2 (hi - lo)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Subtraction",
"LowParse.BitFields.get_bitfield_empty",
"Prims.unit",
"LowParse.BitFields.mod_1",
"Prims.op_Division",
"Prims.pow2",
"FStar.Pervasives.assert_norm",
"Prims.eq2",
"Prims._assert",
"Prims.bool",
"LowParse.BitFields.get_bitfield_full",
"FStar.Math.Lemmas.small_mod",
"LowParse.BitFields.div_1",
"Prims.l_or",
"Prims.op_GreaterThan",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt.shift_right_value_lemma",
"FStar.UInt.logand_mask",
"FStar.UInt.shift_right",
"FStar.Math.Lemmas.multiple_division_lemma",
"LowParse.BitFields.bitfield_mask",
"FStar.UInt.shift_right_logand_lemma",
"Prims.op_LessThan",
"Prims.l_True",
"Prims.squash",
"LowParse.BitFields.get_bitfield",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo)) =
| if hi - lo = 0
then
(assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo)
else
if hi - lo = tot
then
(assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x)
else
(assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_eq | val set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo)) | val set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo)) | let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 872,
"start_col": 0,
"start_line": 868
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo)
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.set_bitfield x lo hi v ==
FStar.UInt.logor (FStar.UInt.logand x
(FStar.UInt.lognot (FStar.UInt.shift_left (FStar.UInt.shift_right (FStar.UInt.lognot 0)
(tot - (hi - lo)))
lo)))
(FStar.UInt.shift_left v lo)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"LowParse.BitFields.bitfield_mask_eq_2",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"LowParse.BitFields.set_bitfield",
"FStar.UInt.logor",
"FStar.UInt.logand",
"FStar.UInt.lognot",
"FStar.UInt.shift_left",
"FStar.UInt.shift_right",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let set_bitfield_eq
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v ==
(x `U.logand` (U.lognot (((U.lognot 0) `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)))
`U.logor`
(v `U.shift_left` lo)) =
| bitfield_mask_eq_2 tot lo hi | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_bound | val set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound) | val set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound) | let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 770,
"start_col": 0,
"start_line": 755
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
bound: Prims.nat{bound <= tot /\ x < Prims.pow2 bound} ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= bound} ->
v: LowParse.BitFields.ubitfield tot (hi - lo)
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.set_bitfield x lo hi v < Prims.pow2 bound) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"Prims.pow2",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"Prims.op_Equality",
"Prims.int",
"LowParse.BitFields.set_bitfield_empty",
"Prims.bool",
"LowParse.BitFields.set_bitfield_size",
"Prims.unit",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.l_True",
"Prims.squash",
"LowParse.BitFields.set_bitfield",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat{bound <= tot /\ x < pow2 bound})
(lo: nat)
(hi: nat{lo <= hi /\ hi <= bound})
(v: ubitfield tot (hi - lo))
: Lemma (set_bitfield x lo hi v < pow2 bound) =
| if bound = 0
then set_bitfield_empty x lo v
else
(M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_get_bitfield | val set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x) | val set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x) | let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 625,
"start_col": 0,
"start_line": 615
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures LowParse.BitFields.set_bitfield x lo hi (LowParse.BitFields.get_bitfield x lo hi) == x) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.set_bitfield",
"LowParse.BitFields.get_bitfield",
"Prims.op_LessThan",
"Prims.op_AmpAmp",
"LowParse.BitFields.nth_get_bitfield",
"Prims.op_Subtraction",
"Prims.bool",
"Prims.unit",
"LowParse.BitFields.nth_set_bitfield",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
: Lemma (set_bitfield x lo hi (get_bitfield x lo hi) == x) =
| eq_nth (set_bitfield x lo hi (get_bitfield x lo hi))
x
(fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi then nth_get_bitfield x lo hi (i - lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_eq_2 | val get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) | val get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) | let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 856,
"start_col": 0,
"start_line": 846
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield x lo hi ==
FStar.UInt.shift_right (FStar.UInt.shift_left x (tot - hi)) (tot - hi + lo)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"FStar.UInt.shift_right",
"FStar.UInt.shift_left",
"Prims.op_Subtraction",
"Prims.op_Addition",
"Prims.op_LessThan",
"LowParse.BitFields.nth_shift_left",
"Prims.bool",
"Prims.unit",
"Prims.int",
"LowParse.BitFields.nth_shift_right",
"LowParse.BitFields.nth_get_bitfield",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_eq_2 (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) =
| eq_nth (get_bitfield x lo hi)
((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
(fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot then nth_shift_left x (tot - hi) j) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_is_zero | val bitfield_is_zero (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield x lo hi == 0 <==> x `U.logand` (bitfield_mask tot lo hi) == 0) | val bitfield_is_zero (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield x lo hi == 0 <==> x `U.logand` (bitfield_mask tot lo hi) == 0) | let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g () | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 565,
"start_col": 0,
"start_line": 529
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield x lo hi == 0 <==>
FStar.UInt.logand x (LowParse.BitFields.bitfield_mask tot lo hi) == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.eq2",
"Prims.int",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowParse.BitFields.eq_nth",
"Prims.op_LessThan",
"Prims.op_AmpAmp",
"LowParse.BitFields.nth_zero",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_get_bitfield",
"Prims.bool",
"LowParse.BitFields.nth_bitfield_mask",
"LowParse.BitFields.nth_logand",
"LowParse.BitFields.bitfield_mask",
"Prims.op_Addition",
"LowParse.BitFields.ubitfield",
"LowParse.BitFields.get_bitfield",
"FStar.UInt.logand",
"Prims.l_True",
"Prims.l_iff"
] | [] | false | false | true | false | false | let bitfield_is_zero (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (get_bitfield x lo hi == 0 <==> x `U.logand` (bitfield_mask tot lo hi) == 0) =
| let y = x `U.logand` (bitfield_mask tot lo hi) in
let z = get_bitfield x lo hi in
let f () : Lemma (requires (y == 0)) (ensures (z == 0)) =
eq_nth z
0
(fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo then nth_zero tot (i + lo))
in
let g () : Lemma (requires (z == 0)) (ensures (y == 0)) =
eq_nth y
0
(fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then
(nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)))
in
Classical.move_requires f ();
Classical.move_requires g () | false |
LowParse.BitFields.fst | LowParse.BitFields.lt_pow2_get_bitfield_hi | val lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0)) | val lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0)) | let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 463,
"start_col": 0,
"start_line": 444
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> mi: Prims.nat{mi <= tot}
-> FStar.Pervasives.Lemma (requires x < Prims.pow2 mi)
(ensures LowParse.BitFields.get_bitfield x mi tot == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"LowParse.BitFields.get_bitfield_zero",
"Prims.bool",
"Prims.op_LessThan",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"LowParse.BitFields.nth_pow2_minus_one",
"Prims.unit",
"LowParse.BitFields.nth_get_bitfield",
"FStar.UInt.logand",
"Prims.op_Subtraction",
"Prims.pow2",
"LowParse.BitFields.nth_zero",
"FStar.UInt.logand_mask",
"FStar.Math.Lemmas.modulo_lemma",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let lt_pow2_get_bitfield_hi (#tot: pos) (x: U.uint_t tot) (mi: nat{mi <= tot})
: Lemma (requires (x < pow2 mi)) (ensures (get_bitfield x mi tot == 0)) =
| if mi = 0
then get_bitfield_zero tot mi tot
else
if mi < tot
then
(M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot)
0
(fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i)) | false |
LowParse.BitFields.fst | LowParse.BitFields.u64_shift_right | val u64_shift_right (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_right` (U32.v amount)}) | val u64_shift_right (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_right` (U32.v amount)}) | let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 59,
"end_line": 897,
"start_col": 0,
"start_line": 893
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 64}
-> y:
FStar.UInt64.t
{FStar.UInt64.v y == FStar.UInt.shift_right (FStar.UInt64.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt64.__uint_to_t",
"Prims.bool",
"FStar.UInt64.shift_right",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt.shift_right"
] | [] | false | false | false | false | false | let u64_shift_right (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_right` (U32.v amount)}) =
| if amount = 64ul then 0uL else x `U64.shift_right` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_zero_inner | val get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0)) | val get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0)) | let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f () | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 525,
"start_col": 0,
"start_line": 504
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 2,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
lo': Prims.nat{lo <= lo'} ->
hi': Prims.nat{lo' <= hi' /\ hi' <= hi}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield x lo hi == 0 ==>
LowParse.BitFields.get_bitfield x lo' hi' == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.eq2",
"Prims.int",
"LowParse.BitFields.get_bitfield",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowParse.BitFields.eq_nth",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_zero",
"Prims.op_Addition",
"LowParse.BitFields.nth_get_bitfield",
"Prims.bool",
"Prims.l_True",
"Prims.l_imp"
] | [] | false | false | true | false | false | let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(lo': nat{lo <= lo'})
(hi': nat{lo' <= hi' /\ hi' <= hi})
: Lemma (ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0)) =
| let f () : Lemma (requires (get_bitfield x lo hi == 0)) (ensures (get_bitfield x lo' hi' == 0)) =
eq_nth (get_bitfield x lo' hi')
0
(fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo')
then
(nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)))
in
Classical.move_requires f () | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_hi_lt_pow2 | val get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi)) | val get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi)) | let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 487,
"start_col": 0,
"start_line": 465
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot -> mi: Prims.nat{mi <= tot}
-> FStar.Pervasives.Lemma (requires LowParse.BitFields.get_bitfield x mi tot == 0)
(ensures x < Prims.pow2 mi) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"LowParse.BitFields.get_bitfield_full",
"Prims.bool",
"Prims.op_LessThan",
"FStar.Math.Lemmas.lemma_mod_lt",
"Prims.pow2",
"Prims.unit",
"FStar.UInt.logand_mask",
"LowParse.BitFields.eq_nth",
"FStar.UInt.logand",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_zero",
"LowParse.BitFields.nth_get_bitfield",
"LowParse.BitFields.nth_pow2_minus_one",
"FStar.Math.Lemmas.pow2_le_compat",
"Prims.eq2",
"LowParse.BitFields.get_bitfield",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_hi_lt_pow2 (#tot: pos) (x: U.uint_t tot) (mi: nat{mi <= tot})
: Lemma (requires (get_bitfield x mi tot == 0)) (ensures (x < pow2 mi)) =
| if mi = 0
then get_bitfield_full x
else
if mi < tot
then
(M.pow2_le_compat tot mi;
eq_nth x
(x `U.logand` (pow2 mi - 1))
(fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then
(nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)));
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_set_bitfield_get_bitfield | val set_bitfield_set_bitfield_get_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= hi - lo }) (v' : ubitfield tot (hi' - lo'))
: Lemma
( let v = set_bitfield (get_bitfield x lo hi) lo' hi' v' in
v < pow2 (hi - lo) /\
set_bitfield x lo hi v == set_bitfield x (lo + lo') (lo + hi') v' ) | val set_bitfield_set_bitfield_get_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= hi - lo }) (v' : ubitfield tot (hi' - lo'))
: Lemma
( let v = set_bitfield (get_bitfield x lo hi) lo' hi' v' in
v < pow2 (hi - lo) /\
set_bitfield x lo hi v == set_bitfield x (lo + lo') (lo + hi') v' ) | let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 797,
"start_col": 0,
"start_line": 774
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 64,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
lo': Prims.nat ->
hi': Prims.nat{lo' <= hi' /\ hi' <= hi - lo} ->
v': LowParse.BitFields.ubitfield tot (hi' - lo')
-> FStar.Pervasives.Lemma
(ensures
(let v =
LowParse.BitFields.set_bitfield (LowParse.BitFields.get_bitfield x lo hi) lo' hi' v'
in
v < Prims.pow2 (hi - lo) /\
LowParse.BitFields.set_bitfield x lo hi v ==
LowParse.BitFields.set_bitfield x (lo + lo') (lo + hi') v')) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"LowParse.BitFields.ubitfield",
"LowParse.BitFields.eq_nth",
"Prims.op_LessThan",
"Prims.op_AmpAmp",
"Prims.bool",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"Prims.unit",
"LowParse.BitFields.nth_get_bitfield",
"LowParse.BitFields.nth",
"LowParse.BitFields.get_bitfield",
"LowParse.BitFields.nth_set_bitfield",
"LowParse.BitFields.set_bitfield",
"LowParse.BitFields.set_bitfield_bound"
] | [] | false | false | true | false | false | let set_bitfield_set_bitfield_get_bitfield #tot x lo hi lo' hi' v' =
| set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v';
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1
x2
(fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i;
if lo <= i && i < hi
then
(assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then ()
else
(assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)))) | false |
LowParse.BitFields.fst | LowParse.BitFields.not_bitfield_mask64 | val not_bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == not_bitfield_mask 64 lo hi}) | val not_bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == not_bitfield_mask 64 lo hi}) | let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 907,
"start_col": 0,
"start_line": 906
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 64}
-> x: FStar.UInt64.t{FStar.UInt64.v x == LowParse.BitFields.not_bitfield_mask 64 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.lognot",
"LowParse.BitFields.bitfield_mask64",
"FStar.UInt64.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.v",
"LowParse.BitFields.not_bitfield_mask"
] | [] | false | false | false | false | false | let not_bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == not_bitfield_mask 64 lo hi}) =
| U64.lognot (bitfield_mask64 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.not_bitfield_mask32 | val not_bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == not_bitfield_mask 32 lo hi}) | val not_bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == not_bitfield_mask 32 lo hi}) | let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 984,
"start_col": 0,
"start_line": 983
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 32}
-> x: FStar.UInt32.t{FStar.UInt32.v x == LowParse.BitFields.not_bitfield_mask 32 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.lognot",
"LowParse.BitFields.bitfield_mask32",
"FStar.UInt32.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.v",
"LowParse.BitFields.not_bitfield_mask"
] | [] | false | false | false | false | false | let not_bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == not_bitfield_mask 32 lo hi}) =
| U32.lognot (bitfield_mask32 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield64 | val get_bitfield64 (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) lo hi}) | val get_bitfield64 (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) lo hi}) | let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 77,
"end_line": 903,
"start_col": 0,
"start_line": 900
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 64}
-> y: FStar.UInt64.t{FStar.UInt64.v y == LowParse.BitFields.get_bitfield (FStar.UInt64.v x) lo hi} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.u64_shift_right",
"FStar.UInt64.logand",
"LowParse.BitFields.bitfield_mask64",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield64 (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) lo hi}) =
| (x `U64.logand` (bitfield_mask64 lo hi)) `u64_shift_right` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_get_bitfield | val get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi')) | val get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi')) | let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 501,
"start_col": 0,
"start_line": 489
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
lo': Prims.nat ->
hi': Prims.nat{lo' <= hi' /\ hi' <= hi - lo}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield (LowParse.BitFields.get_bitfield x lo hi) lo' hi' ==
LowParse.BitFields.get_bitfield x (lo + lo') (lo + hi')) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_Addition",
"Prims.op_LessThan",
"LowParse.BitFields.nth_get_bitfield",
"Prims.bool",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.l_or",
"Prims.pow2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(lo': nat)
(hi': nat{lo' <= hi' /\ hi' <= hi - lo})
: Lemma (get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi')) =
| eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi')
(get_bitfield x (lo + lo') (lo + hi'))
(fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i;
if i < hi' - lo' then nth_get_bitfield x lo hi (i + lo')) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask64 | val bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == bitfield_mask 64 lo hi}) | val bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == bitfield_mask 64 lo hi}) | let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 890,
"start_col": 0,
"start_line": 884
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 64}
-> x: FStar.UInt64.t{FStar.UInt64.v x == LowParse.BitFields.bitfield_mask 64 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"FStar.UInt64.__uint_to_t",
"Prims.bool",
"FStar.UInt64.shift_left",
"FStar.UInt64.shift_right",
"FStar.UInt64.lognot",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"FStar.UInt64.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"LowParse.BitFields.bitfield_mask"
] | [] | false | false | false | false | false | let bitfield_mask64 (lo: nat) (hi: nat{lo <= hi /\ hi <= 64})
: Tot (x: U64.t{U64.v x == bitfield_mask 64 lo hi}) =
| if lo = hi
then 0uL
else
(bitfield_mask_eq_2 64 lo hi;
((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo))))
`U64.shift_left`
(U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq16_lhs | val bitfield_eq16_lhs (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16}) : Tot U16.t | val bitfield_eq16_lhs (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16}) : Tot U16.t | let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 38,
"end_line": 1081,
"start_col": 0,
"start_line": 1078
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 16} -> FStar.UInt16.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt16.logand",
"LowParse.BitFields.bitfield_mask16"
] | [] | false | false | false | false | false | let bitfield_eq16_lhs (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16}) : Tot U16.t =
| x `U16.logand` (bitfield_mask16 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.u64_shift_left | val u64_shift_left (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_left` (U32.v amount)}) | val u64_shift_left (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_left` (U32.v amount)}) | let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 914,
"start_col": 0,
"start_line": 910
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 64}
-> y:
FStar.UInt64.t
{FStar.UInt64.v y == FStar.UInt.shift_left (FStar.UInt64.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt64.__uint_to_t",
"Prims.bool",
"FStar.UInt64.shift_left",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt.shift_left"
] | [] | false | false | false | false | false | let u64_shift_left (x: U64.t) (amount: U32.t{U32.v amount <= 64})
: Tot (y: U64.t{U64.v y == (U64.v x) `U.shift_left` (U32.v amount)}) =
| if amount = 64ul then 0uL else x `U64.shift_left` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield32 | val get_bitfield32 (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) lo hi}) | val get_bitfield32 (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) lo hi}) | let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 77,
"end_line": 980,
"start_col": 0,
"start_line": 977
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 32}
-> y: FStar.UInt32.t{FStar.UInt32.v y == LowParse.BitFields.get_bitfield (FStar.UInt32.v x) lo hi} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.u32_shift_right",
"FStar.UInt32.logand",
"LowParse.BitFields.bitfield_mask32",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"FStar.UInt32.v",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield32 (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) lo hi}) =
| (x `U32.logand` (bitfield_mask32 lo hi)) `u32_shift_right` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield64 | val set_bitfield64
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) lo hi (U64.v v)}) | val set_bitfield64
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) lo hi (U64.v v)}) | let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 94,
"end_line": 921,
"start_col": 0,
"start_line": 917
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt64.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 64} ->
v: FStar.UInt64.t{FStar.UInt64.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt64.t
{ FStar.UInt64.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt64.v x) lo hi (FStar.UInt64.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt64.logor",
"FStar.UInt64.logand",
"LowParse.BitFields.not_bitfield_mask64",
"LowParse.BitFields.u64_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield64
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) lo hi (U64.v v)}) =
| (x `U64.logand` (not_bitfield_mask64 lo hi)) `U64.logor` (v `u64_shift_left` (U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask32 | val bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == bitfield_mask 32 lo hi}) | val bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == bitfield_mask 32 lo hi}) | let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 967,
"start_col": 0,
"start_line": 961
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 32}
-> x: FStar.UInt32.t{FStar.UInt32.v x == LowParse.BitFields.bitfield_mask 32 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"FStar.UInt32.shift_left",
"FStar.UInt32.shift_right",
"FStar.UInt32.lognot",
"FStar.UInt32.sub",
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"FStar.UInt32.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"FStar.UInt32.v",
"LowParse.BitFields.bitfield_mask"
] | [] | false | false | false | false | false | let bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == bitfield_mask 32 lo hi}) =
| if lo = hi
then 0ul
else
(bitfield_mask_eq_2 32 lo hi;
((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo))))
`U32.shift_left`
(U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask_eq_2 | val bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(bitfield_mask tot lo hi ==
U.shift_left #tot ((U.lognot 0) `U.shift_right` (tot - (hi - lo))) lo) | val bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(bitfield_mask tot lo hi ==
U.shift_left #tot ((U.lognot 0) `U.shift_right` (tot - (hi - lo))) lo) | let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo)) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 65,
"end_line": 866,
"start_col": 0,
"start_line": 859
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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 | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.bitfield_mask tot lo hi ==
FStar.UInt.shift_left (FStar.UInt.shift_right (FStar.UInt.lognot 0) (tot - (hi - lo))) lo) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt.shift_right_value_lemma",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"FStar.UInt.lemma_lognot_value_mod",
"LowParse.BitFields.pow2_m_minus_one_eq",
"LowParse.BitFields.bitfield_mask_eq",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.UInt.uint_t",
"LowParse.BitFields.bitfield_mask",
"FStar.UInt.shift_left",
"FStar.UInt.shift_right",
"FStar.UInt.lognot",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma
(bitfield_mask tot lo hi ==
U.shift_left #tot ((U.lognot 0) `U.shift_right` (tot - (hi - lo))) lo) =
| bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.u32_shift_right | val u32_shift_right (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_right` (U32.v amount)}) | val u32_shift_right (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_right` (U32.v amount)}) | let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 59,
"end_line": 974,
"start_col": 0,
"start_line": 970
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 32}
-> y:
FStar.UInt32.t
{FStar.UInt32.v y == FStar.UInt.shift_right (FStar.UInt32.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"FStar.UInt32.shift_right",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"FStar.UInt.shift_right"
] | [] | false | false | false | false | false | let u32_shift_right (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_right` (U32.v amount)}) =
| if amount = 32ul then 0ul else x `U32.shift_right` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask16 | val bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == bitfield_mask 16 lo hi}) | val bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == bitfield_mask 16 lo hi}) | let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 1044,
"start_col": 0,
"start_line": 1038
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 16}
-> x: FStar.UInt16.t{FStar.UInt16.v x == LowParse.BitFields.bitfield_mask 16 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"FStar.UInt16.__uint_to_t",
"Prims.bool",
"FStar.UInt16.shift_left",
"FStar.UInt16.shift_right",
"FStar.UInt16.lognot",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"FStar.UInt16.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.v",
"LowParse.BitFields.bitfield_mask"
] | [] | false | false | false | false | false | let bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == bitfield_mask 16 lo hi}) =
| if lo = hi
then 0us
else
(bitfield_mask_eq_2 16 lo hi;
((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo))))
`U16.shift_left`
(U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_gen64 | val get_bitfield_gen64 (x: U64.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi)}) | val get_bitfield_gen64 (x: U64.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi)}) | let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 95,
"end_line": 948,
"start_col": 0,
"start_line": 944
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt64.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 64}
-> y:
FStar.UInt64.t
{ FStar.UInt64.v y ==
LowParse.BitFields.get_bitfield (FStar.UInt64.v x) (FStar.UInt32.v lo) (FStar.UInt32.v hi) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.shift_right",
"FStar.UInt64.shift_left",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.add",
"Prims.unit",
"LowParse.BitFields.get_bitfield_eq_2",
"FStar.UInt64.v",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield_gen64 (x: U64.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t{U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi)}) =
| get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield32 | val set_bitfield32
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) lo hi (U32.v v)}) | val set_bitfield32
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) lo hi (U32.v v)}) | let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 94,
"end_line": 998,
"start_col": 0,
"start_line": 994
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt32.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 32} ->
v: FStar.UInt32.t{FStar.UInt32.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt32.t
{ FStar.UInt32.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt32.v x) lo hi (FStar.UInt32.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt32.logor",
"FStar.UInt32.logand",
"LowParse.BitFields.not_bitfield_mask32",
"LowParse.BitFields.u32_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield32
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) lo hi (U32.v v)}) =
| (x `U32.logand` (not_bitfield_mask32 lo hi)) `U32.logor` (v `u32_shift_left` (U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq8_lhs | val bitfield_eq8_lhs (x: U8.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 8}) : Tot U8.t | val bitfield_eq8_lhs (x: U8.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 8}) : Tot U8.t | let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 1190,
"start_col": 0,
"start_line": 1187
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt8.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 8} -> FStar.UInt8.t | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt8.logand",
"LowParse.BitFields.bitfield_mask8"
] | [] | false | false | false | false | false | let bitfield_eq8_lhs (x: U8.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 8}) : Tot U8.t =
| x `U8.logand` (bitfield_mask8 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.u16_shift_right | val u16_shift_right (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_right` (U32.v amount)}) | val u16_shift_right (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_right` (U32.v amount)}) | let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 59,
"end_line": 1051,
"start_col": 0,
"start_line": 1047
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 16}
-> y:
FStar.UInt16.t
{FStar.UInt16.v y == FStar.UInt.shift_right (FStar.UInt16.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt16.__uint_to_t",
"Prims.bool",
"FStar.UInt16.shift_right",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt.shift_right"
] | [] | false | false | false | false | false | let u16_shift_right (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_right` (U32.v amount)}) =
| if amount = 16ul then 0us else x `U16.shift_right` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.not_bitfield_mask16 | val not_bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == not_bitfield_mask 16 lo hi}) | val not_bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == not_bitfield_mask 16 lo hi}) | let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 1061,
"start_col": 0,
"start_line": 1060
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 16}
-> x: FStar.UInt16.t{FStar.UInt16.v x == LowParse.BitFields.not_bitfield_mask 16 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt16.lognot",
"LowParse.BitFields.bitfield_mask16",
"FStar.UInt16.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.v",
"LowParse.BitFields.not_bitfield_mask"
] | [] | false | false | false | false | false | let not_bitfield_mask16 (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (x: U16.t{U16.v x == not_bitfield_mask 16 lo hi}) =
| U16.lognot (bitfield_mask16 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_gen32 | val get_bitfield_gen32 (x lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi)}) | val get_bitfield_gen32 (x lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi)}) | let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 95,
"end_line": 1021,
"start_col": 0,
"start_line": 1017
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt32.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 32}
-> y:
FStar.UInt32.t
{ FStar.UInt32.v y ==
LowParse.BitFields.get_bitfield (FStar.UInt32.v x) (FStar.UInt32.v lo) (FStar.UInt32.v hi) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.shift_right",
"FStar.UInt32.shift_left",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.add",
"Prims.unit",
"LowParse.BitFields.get_bitfield_eq_2",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield_gen32 (x lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t{U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi)}) =
| get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.u16_shift_left | val u16_shift_left (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_left` (U32.v amount)}) | val u16_shift_left (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_left` (U32.v amount)}) | let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 1068,
"start_col": 0,
"start_line": 1064
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 16}
-> y:
FStar.UInt16.t
{FStar.UInt16.v y == FStar.UInt.shift_left (FStar.UInt16.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt16.__uint_to_t",
"Prims.bool",
"FStar.UInt16.shift_left",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.v",
"FStar.UInt.shift_left"
] | [] | false | false | false | false | false | let u16_shift_left (x: U16.t) (amount: U32.t{U32.v amount <= 16})
: Tot (y: U16.t{U16.v y == (U16.v x) `U.shift_left` (U32.v amount)}) =
| if amount = 16ul then 0us else x `U16.shift_left` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.u32_shift_left | val u32_shift_left (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_left` (U32.v amount)}) | val u32_shift_left (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_left` (U32.v amount)}) | let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 58,
"end_line": 991,
"start_col": 0,
"start_line": 987
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 32}
-> y:
FStar.UInt32.t
{FStar.UInt32.v y == FStar.UInt.shift_left (FStar.UInt32.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"FStar.UInt32.shift_left",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"FStar.UInt.shift_left"
] | [] | false | false | false | false | false | let u32_shift_left (x: U32.t) (amount: U32.t{U32.v amount <= 32})
: Tot (y: U32.t{U32.v y == (U32.v x) `U.shift_left` (U32.v amount)}) =
| if amount = 32ul then 0ul else x `U32.shift_left` amount | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq64_rhs | val bitfield_eq64_rhs
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v}) | val bitfield_eq64_rhs
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v}) | let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 37,
"end_line": 939,
"start_col": 0,
"start_line": 932
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt64.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 64} ->
v: FStar.UInt64.t{FStar.UInt64.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt64.t
{ LowParse.BitFields.bitfield_eq64_lhs x lo hi == y <==>
LowParse.BitFields.get_bitfield64 x lo hi == v } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.u64_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_eq_shift",
"FStar.UInt64.n",
"Prims.l_iff",
"Prims.eq2",
"LowParse.BitFields.bitfield_eq64_lhs",
"LowParse.BitFields.get_bitfield64"
] | [] | false | false | false | false | false | let bitfield_eq64_rhs
(x: U64.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 64})
(v: U64.t{U64.v v < pow2 (hi - lo)})
: Tot (y: U64.t{bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v}) =
| [@@ inline_let ]let _ = bitfield_eq_shift (U64.v x) lo hi (U64.v v) in
v `u64_shift_left` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.not_bitfield_mask8 | val not_bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == not_bitfield_mask 8 lo hi}) | val not_bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == not_bitfield_mask 8 lo hi}) | let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 34,
"end_line": 1164,
"start_col": 0,
"start_line": 1163
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 8}
-> x: FStar.UInt8.t{FStar.UInt8.v x == LowParse.BitFields.not_bitfield_mask 8 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt8.lognot",
"LowParse.BitFields.bitfield_mask8",
"FStar.UInt8.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.v",
"LowParse.BitFields.not_bitfield_mask"
] | [] | false | false | false | false | false | let not_bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == not_bitfield_mask 8 lo hi}) =
| U8.lognot (bitfield_mask8 lo hi) | false |
LowParse.BitFields.fst | LowParse.BitFields.u8_shift_right | val u8_shift_right (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_right` (U32.v amount)}) | val u8_shift_right (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_right` (U32.v amount)}) | let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 1127,
"start_col": 0,
"start_line": 1120
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt8.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 8}
-> y:
FStar.UInt8.t
{FStar.UInt8.v y == FStar.UInt.shift_right (FStar.UInt8.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.v",
"FStar.UInt.shift_right",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt8.__uint_to_t",
"Prims.bool",
"FStar.UInt8.shift_right",
"FStar.UInt8.n"
] | [] | false | false | false | false | false | let u8_shift_right (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_right` (U32.v amount)}) =
| let y = if amount = 8ul then 0uy else x `U8.shift_right` amount in
y | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq32_rhs | val bitfield_eq32_rhs
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v}) | val bitfield_eq32_rhs
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v}) | let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 37,
"end_line": 1014,
"start_col": 0,
"start_line": 1007
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt32.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 32} ->
v: FStar.UInt32.t{FStar.UInt32.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt32.t
{ LowParse.BitFields.bitfield_eq32_lhs x lo hi == y <==>
LowParse.BitFields.get_bitfield32 x lo hi == v } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.u32_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_eq_shift",
"FStar.UInt32.n",
"Prims.l_iff",
"Prims.eq2",
"LowParse.BitFields.bitfield_eq32_lhs",
"LowParse.BitFields.get_bitfield32"
] | [] | false | false | false | false | false | let bitfield_eq32_rhs
(x: U32.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 32})
(v: U32.t{U32.v v < pow2 (hi - lo)})
: Tot (y: U32.t{bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v}) =
| [@@ inline_let ]let _ = bitfield_eq_shift (U32.v x) lo hi (U32.v v) in
v `u32_shift_left` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.uint64 | val uint64 : uint_t 64 U64.t | val uint64 : uint_t 64 U64.t | let uint64 : uint_t 64 U64.t = {
v = U64.v;
uint_to_t = U64.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen64 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen64 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield64 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield64 x lo hi z);
logor = (fun x y -> U64.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq64_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq64_rhs x lo hi z);
} | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 1216,
"start_col": 0,
"start_line": 1204
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v
inline_for_extraction
let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi
inline_for_extraction
let bitfield_eq8_rhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U8.v x) lo hi (U8.v v)
in
v `u8_shift_left` U32.uint_to_t lo
inline_for_extraction | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.BitFields.uint_t 64 FStar.UInt64.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.BitFields.Mkuint_t",
"FStar.UInt64.n",
"FStar.UInt64.t",
"FStar.UInt64.v",
"FStar.UInt64.uint_to_t",
"FStar.UInt.uint_t",
"Prims.unit",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.get_bitfield_gen64",
"Prims.eq2",
"LowParse.BitFields.get_bitfield",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.set_bitfield_gen64",
"LowParse.BitFields.set_bitfield",
"Prims.nat",
"LowParse.BitFields.get_bitfield64",
"LowParse.BitFields.set_bitfield64",
"FStar.UInt64.logor",
"FStar.UInt.logor",
"LowParse.BitFields.bitfield_eq64_lhs",
"LowParse.BitFields.bitfield_eq64_rhs",
"Prims.l_iff",
"LowParse.BitFields.uint_t"
] | [] | false | false | false | false | false | let uint64:uint_t 64 U64.t =
| {
v = U64.v;
uint_to_t = U64.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen64 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen64 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield64 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield64 x lo hi z);
logor = (fun x y -> U64.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq64_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq64_rhs x lo hi z)
} | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield16 | val get_bitfield16 (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) lo hi}) | val get_bitfield16 (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) lo hi}) | let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 77,
"end_line": 1057,
"start_col": 0,
"start_line": 1054
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt16.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 16}
-> y: FStar.UInt16.t{FStar.UInt16.v y == LowParse.BitFields.get_bitfield (FStar.UInt16.v x) lo hi} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.u16_shift_right",
"FStar.UInt16.logand",
"LowParse.BitFields.bitfield_mask16",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"FStar.UInt16.v",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield16 (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) lo hi}) =
| (x `U16.logand` (bitfield_mask16 lo hi)) `u16_shift_right` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_gen16 | val get_bitfield_gen16 (x: U16.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi)}) | val get_bitfield_gen16 (x: U16.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi)}) | let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 57,
"end_line": 1100,
"start_col": 0,
"start_line": 1094
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt16.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 16}
-> y:
FStar.UInt16.t
{ FStar.UInt16.v y ==
LowParse.BitFields.get_bitfield (FStar.UInt16.v x) (FStar.UInt32.v lo) (FStar.UInt32.v hi) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt16.shift_right",
"FStar.UInt32.add",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt16.shift_left",
"Prims.unit",
"LowParse.BitFields.get_bitfield_eq_2",
"FStar.UInt16.v",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield_gen16 (x: U16.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t{U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi)}) =
| get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
let bf:U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_gen64 | val set_bitfield_gen64
(x: U64.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t{U64.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v)}) | val set_bitfield_gen64
(x: U64.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t{U64.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v)}) | let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 159,
"end_line": 956,
"start_col": 0,
"start_line": 951
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt64.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 64} ->
v: FStar.UInt64.t{FStar.UInt64.v v < Prims.pow2 (FStar.UInt32.v hi - FStar.UInt32.v lo)}
-> y:
FStar.UInt64.t
{ FStar.UInt64.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt64.v x)
(FStar.UInt32.v lo)
(FStar.UInt32.v hi)
(FStar.UInt64.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt64.logor",
"FStar.UInt64.logand",
"FStar.UInt64.lognot",
"FStar.UInt64.shift_left",
"FStar.UInt64.shift_right",
"FStar.UInt64.__uint_to_t",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield_gen64
(x: U64.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t{U64.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U64.t{U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v)}) =
| bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x
`U64.logand`
(U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo)))
`U64.shift_left`
lo)))
`U64.logor`
(v `U64.shift_left` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_mask8 | val bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == bitfield_mask 8 lo hi}) | val bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == bitfield_mask 8 lo hi}) | let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 1117,
"start_col": 0,
"start_line": 1111
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 8}
-> x: FStar.UInt8.t{FStar.UInt8.v x == LowParse.BitFields.bitfield_mask 8 lo hi} | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_Equality",
"FStar.UInt8.__uint_to_t",
"Prims.bool",
"FStar.UInt8.shift_left",
"FStar.UInt8.shift_right",
"FStar.UInt8.lognot",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"FStar.UInt8.t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.n",
"FStar.UInt8.v",
"LowParse.BitFields.bitfield_mask"
] | [] | false | false | false | false | false | let bitfield_mask8 (lo: nat) (hi: nat{lo <= hi /\ hi <= 8})
: Tot (x: U8.t{U8.v x == bitfield_mask 8 lo hi}) =
| if lo = hi
then 0uy
else
(bitfield_mask_eq_2 8 lo hi;
((U8.lognot 0uy) `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo))))
`U8.shift_left`
(U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_gen8 | val set_bitfield_gen8
(x: U8.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t{U8.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v)}) | val set_bitfield_gen8
(x: U8.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t{U8.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v)}) | let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 1153,
"start_col": 0,
"start_line": 1140
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt8.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 8} ->
v: FStar.UInt8.t{FStar.UInt8.v v < Prims.pow2 (FStar.UInt32.v hi - FStar.UInt32.v lo)}
-> y:
FStar.UInt8.t
{ FStar.UInt8.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt8.v x)
(FStar.UInt32.v lo)
(FStar.UInt32.v hi)
(FStar.UInt8.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt8.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt8.logor",
"FStar.UInt8.shift_left",
"FStar.UInt8.logand",
"FStar.UInt8.lognot",
"FStar.UInt8.shift_right",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt8.__uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield_gen8
(x: U8.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t{U8.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v)}) =
| bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6 | false |
LowParse.BitFields.fst | LowParse.BitFields.uint32 | val uint32 : uint_t 32 U32.t | val uint32 : uint_t 32 U32.t | let uint32 : uint_t 32 U32.t = {
v = U32.v;
uint_to_t = U32.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen32 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen32 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield32 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield32 x lo hi z);
logor = (fun x y -> U32.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq32_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq32_rhs x lo hi z);
} | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 1235,
"start_col": 0,
"start_line": 1223
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v
inline_for_extraction
let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi
inline_for_extraction
let bitfield_eq8_rhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U8.v x) lo hi (U8.v v)
in
v `u8_shift_left` U32.uint_to_t lo
inline_for_extraction
noextract
let uint64 : uint_t 64 U64.t = {
v = U64.v;
uint_to_t = U64.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen64 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen64 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield64 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield64 x lo hi z);
logor = (fun x y -> U64.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq64_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq64_rhs x lo hi z);
}
let uint64_v_eq x = ()
let uint64_uint_to_t_eq x = ()
inline_for_extraction | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.BitFields.uint_t 32 FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.BitFields.Mkuint_t",
"FStar.UInt32.n",
"FStar.UInt32.t",
"FStar.UInt32.v",
"FStar.UInt32.uint_to_t",
"FStar.UInt.uint_t",
"Prims.unit",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.get_bitfield_gen32",
"Prims.eq2",
"LowParse.BitFields.get_bitfield",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.set_bitfield_gen32",
"LowParse.BitFields.set_bitfield",
"Prims.nat",
"LowParse.BitFields.get_bitfield32",
"LowParse.BitFields.set_bitfield32",
"FStar.UInt32.logor",
"FStar.UInt.logor",
"LowParse.BitFields.bitfield_eq32_lhs",
"LowParse.BitFields.bitfield_eq32_rhs",
"Prims.l_iff",
"LowParse.BitFields.uint_t"
] | [] | false | false | false | false | false | let uint32:uint_t 32 U32.t =
| {
v = U32.v;
uint_to_t = U32.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen32 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen32 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield32 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield32 x lo hi z);
logor = (fun x y -> U32.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq32_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq32_rhs x lo hi z)
} | false |
Hacl.Impl.K256.PointDouble.fst | Hacl.Impl.K256.PointDouble.point_double_no_alloc | val point_double_no_alloc (out p:point) (tmp:lbuffer uint64 (5ul *! nlimb)) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h tmp /\
eq_or_disjoint out p /\ disjoint out tmp /\ disjoint p tmp /\
point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out |+| loc tmp) h0 h1 /\ point_inv h1 out /\
point_eval h1 out == S.point_double (point_eval h0 p)) | val point_double_no_alloc (out p:point) (tmp:lbuffer uint64 (5ul *! nlimb)) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h tmp /\
eq_or_disjoint out p /\ disjoint out tmp /\ disjoint p tmp /\
point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out |+| loc tmp) h0 h1 /\ point_inv h1 out /\
point_eval h1 out == S.point_double (point_eval h0 p)) | let point_double_no_alloc out p tmp =
let x1, y1, z1 = getx p, gety p, getz p in
let x3, y3, z3 = getx out, gety out, getz out in
let yy = sub tmp 0ul nlimb in
let zz = sub tmp nlimb nlimb in
let bzz3 = sub tmp (2ul *! nlimb) nlimb in
let bzz9 = sub tmp (3ul *! nlimb) nlimb in
let tmp = sub tmp (4ul *! nlimb) nlimb in
let h0 = ST.get () in
fsqr yy y1; //yy = y*y
fsqr zz z1; //zz = z*z
let h1 = ST.get () in
//assert (inv_lazy_reduced2 h1 yy);
//assert (inv_lazy_reduced2 h1 zz);
fmul_small_num x3 x1 (u64 2); //x3 = 2*x
let h2 = ST.get () in
BL.fmul15_lemma (1,1,1,1,2) 2 (as_felem5 h1 x1) (u64 2);
//assert (felem_fits5 (as_felem5 h2 x3) (2,2,2,2,4));
fmul x3 x3 y1; //x3 = xy2 = x3*y = (2*x)*y
calc_z3 y1 z1 z3 yy tmp;
calc_bzz9_tmp yy zz bzz3 bzz9 tmp;
fmul y3 yy zz; //y3 = yy_zz = yy*zz
fmul x3 x3 bzz9; //x3 = x3*bzz9 = xy2*yy_m_bzz9
calc_y3 y3 tmp | {
"file_name": "code/k256/Hacl.Impl.K256.PointDouble.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 16,
"end_line": 145,
"start_col": 0,
"start_line": 117
} | module Hacl.Impl.K256.PointDouble
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module S = Spec.K256
module BL = Hacl.Spec.K256.Field52.Lemmas
open Hacl.K256.Field
open Hacl.Impl.K256.Point
#set-options "--z3rlimit 300 --fuel 0 --ifuel 0"
inline_for_extraction noextract
val calc_z3 (y1 z1 z3 yy tmp:felem) : Stack unit
(requires fun h ->
live h y1 /\ live h z1 /\ live h z3 /\
live h yy /\ live h tmp /\
disjoint y1 z1 /\ disjoint y1 z3 /\ disjoint y1 yy /\
disjoint y1 tmp /\ eq_or_disjoint z1 z3 /\ disjoint z1 yy /\
disjoint z1 tmp /\ disjoint z3 yy /\ disjoint z3 tmp /\ disjoint yy tmp /\
inv_lazy_reduced2 h y1 /\ inv_lazy_reduced2 h z1 /\ inv_lazy_reduced2 h yy)
(ensures fun h0 _ h1 -> modifies (loc tmp |+| loc z3) h0 h1 /\
feval h1 z3 = S.fmul (S.fmul (S.fmul (feval h0 yy) (feval h0 y1)) (feval h0 z1)) 8 /\
inv_lazy_reduced2 h1 z3)
let calc_z3 y1 z1 z3 yy tmp =
fmul tmp yy y1; //tmp = yy*y
fmul z3 tmp z1; //z3 = tmp*z = yy*y*z
let h1 = ST.get () in
//assert (inv_lazy_reduced2 h1 x3);
//assert (inv_lazy_reduced2 h1 tmp);
//assert (inv_lazy_reduced2 h1 z3);
fmul_8_normalize_weak z3 z3 //z3 = z3*8=yy*y*z*8
inline_for_extraction noextract
val calc_bzz9_tmp (yy zz bzz3 bzz9 tmp:felem) : Stack unit
(requires fun h ->
live h yy /\ live h zz /\ live h bzz3 /\ live h bzz9 /\ live h tmp /\
disjoint yy zz /\ disjoint yy bzz3 /\ disjoint yy bzz9 /\
disjoint yy tmp /\ disjoint zz bzz3 /\ disjoint zz bzz9 /\
disjoint zz tmp /\ disjoint bzz3 bzz9 /\ disjoint bzz3 tmp /\
disjoint bzz9 tmp /\
inv_lazy_reduced2 h yy /\ inv_lazy_reduced2 h zz)
(ensures fun h0 _ h1 ->
modifies (loc bzz3 |+| loc bzz9 |+| loc tmp) h0 h1 /\
(let bzz3 = S.fmul (S.fmul 3 S.b) (feval h0 zz) in
feval h1 bzz9 = S.fsub (feval h0 yy) (S.fmul 3 bzz3) /\
feval h1 tmp = S.fmul (feval h1 bzz9) (S.fadd (feval h0 yy) bzz3) /\
inv_lazy_reduced2 h1 tmp /\ felem_fits5 (as_felem5 h1 bzz9) (13,13,13,13,14)))
let calc_bzz9_tmp yy zz bzz3 bzz9 tmp =
fmul_3b_normalize_weak bzz3 zz; //bzz3 = (3*b)*zz
let h1 = ST.get () in
//assert (inv_lazy_reduced2 h1 bzz3);
fmul_small_num bzz9 bzz3 (u64 3); //bzz9 = 3*bzz3
let h2 = ST.get () in
BL.fmul15_lemma (1,1,1,1,2) 3 (as_felem5 h1 bzz3) (u64 3);
//assert (felem_fits5 (as_felem5 h2 bzz9) (3,3,3,3,6));
BL.fsub5_lemma (1,1,1,1,2) (3,3,3,3,6) (as_felem5 h1 yy) (as_felem5 h2 bzz9) (u64 6);
fsub bzz9 yy bzz9 (u64 6); //bzz9 = yy_m_bzz9 = yy-bzz9
let h3 = ST.get () in
//assert (felem_fits5 (as_felem5 h3 bzz9) (13,13,13,13,14));
fadd tmp yy bzz3; //tmp = yy_p_bzz3 = yy+bzz3
let h4 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (1,1,1,1,2) (as_felem5 h1 yy) (as_felem5 h1 bzz3);
//assert (felem_fits5 (as_felem5 h4 tmp) (2,2,2,2,4));
fmul tmp bzz9 tmp //tmp = bzz9*tmp = yy_m_bzz9*yy_p_bzz3
inline_for_extraction noextract
val calc_y3 (y3 tmp: felem) : Stack unit
(requires fun h ->
live h y3 /\ live h tmp /\ disjoint y3 tmp /\
inv_lazy_reduced2 h y3 /\ inv_lazy_reduced2 h tmp)
(ensures fun h0 _ h1 -> modifies (loc y3) h0 h1 /\
feval h1 y3 = S.fadd (feval h0 tmp) (S.fmul (S.fmul 24 S.b) (feval h0 y3))/\
inv_lazy_reduced2 h1 y3)
let calc_y3 y3 tmp =
let h0 = ST.get () in
fmul_small_num y3 y3 (u64 168); //y3 = t = (24*b)*y3 = (24*b)*yy_zz
let h1 = ST.get () in
BL.fmul15_lemma (1,1,1,1,2) 168 (as_felem5 h0 y3) (u64 168);
//assert (felem_fits5 (as_felem5 h1 y3) (168,168,168,168,336));
fadd y3 tmp y3; //y3 = tmp+y3 = yy_m_bzz9*yy_p_bzz3+t
let h2 = ST.get () in
BL.fadd5_lemma (1,1,1,1,2) (168,168,168,168,336) (as_felem5 h0 tmp) (as_felem5 h1 y3);
//assert (felem_fits5 (as_felem5 h2 y3) (169,169,169,169,338));
fnormalize_weak y3 y3;
BL.normalize_weak5_lemma (169,169,169,169,338) (as_felem5 h2 y3)
inline_for_extraction noextract
val point_double_no_alloc (out p:point) (tmp:lbuffer uint64 (5ul *! nlimb)) : Stack unit
(requires fun h ->
live h out /\ live h p /\ live h tmp /\
eq_or_disjoint out p /\ disjoint out tmp /\ disjoint p tmp /\
point_inv h p)
(ensures fun h0 _ h1 -> modifies (loc out |+| loc tmp) h0 h1 /\ point_inv h1 out /\
point_eval h1 out == S.point_double (point_eval h0 p)) | {
"checked_file": "/",
"dependencies": [
"Spec.K256.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.K256.Field52.Lemmas.fsti.checked",
"Hacl.K256.Field.fsti.checked",
"Hacl.Impl.K256.Point.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.K256.PointDouble.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.K256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.K256.Field",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Spec.K256.Field52.Lemmas",
"short_module": "BL"
},
{
"abbrev": true,
"full_module": "Spec.K256",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 300,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
out: Hacl.Impl.K256.Point.point ->
p: Hacl.Impl.K256.Point.point ->
tmp: Lib.Buffer.lbuffer Lib.IntTypes.uint64 (5ul *! Hacl.K256.Field.nlimb)
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.K256.Point.point",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint64",
"Lib.IntTypes.op_Star_Bang",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"FStar.UInt32.__uint_to_t",
"Hacl.K256.Field.nlimb",
"Hacl.K256.Field.felem",
"Hacl.Impl.K256.PointDouble.calc_y3",
"Prims.unit",
"Hacl.K256.Field.fmul",
"Hacl.Impl.K256.PointDouble.calc_bzz9_tmp",
"Hacl.Impl.K256.PointDouble.calc_z3",
"Hacl.Spec.K256.Field52.Lemmas.fmul15_lemma",
"FStar.Pervasives.Native.Mktuple5",
"Prims.nat",
"Hacl.K256.Field.as_felem5",
"Lib.IntTypes.u64",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"Hacl.K256.Field.fmul_small_num",
"Hacl.K256.Field.fsqr",
"Lib.Buffer.lbuffer_t",
"Lib.Buffer.MUT",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Lib.Buffer.sub",
"FStar.Pervasives.Native.tuple3",
"FStar.Pervasives.Native.Mktuple3",
"Hacl.Impl.K256.Point.getz",
"Hacl.Impl.K256.Point.gety",
"Hacl.Impl.K256.Point.getx"
] | [] | false | true | false | false | false | let point_double_no_alloc out p tmp =
| let x1, y1, z1 = getx p, gety p, getz p in
let x3, y3, z3 = getx out, gety out, getz out in
let yy = sub tmp 0ul nlimb in
let zz = sub tmp nlimb nlimb in
let bzz3 = sub tmp (2ul *! nlimb) nlimb in
let bzz9 = sub tmp (3ul *! nlimb) nlimb in
let tmp = sub tmp (4ul *! nlimb) nlimb in
let h0 = ST.get () in
fsqr yy y1;
fsqr zz z1;
let h1 = ST.get () in
fmul_small_num x3 x1 (u64 2);
let h2 = ST.get () in
BL.fmul15_lemma (1, 1, 1, 1, 2) 2 (as_felem5 h1 x1) (u64 2);
fmul x3 x3 y1;
calc_z3 y1 z1 z3 yy tmp;
calc_bzz9_tmp yy zz bzz3 bzz9 tmp;
fmul y3 yy zz;
fmul x3 x3 bzz9;
calc_y3 y3 tmp | false |
LowParse.BitFields.fst | LowParse.BitFields.u8_shift_left | val u8_shift_left (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_left` (U32.v amount)}) | val u8_shift_left (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_left` (U32.v amount)}) | let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 1174,
"start_col": 0,
"start_line": 1167
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt8.t -> amount: FStar.UInt32.t{FStar.UInt32.v amount <= 8}
-> y:
FStar.UInt8.t
{FStar.UInt8.v y == FStar.UInt.shift_left (FStar.UInt8.v x) (FStar.UInt32.v amount)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.v",
"FStar.UInt.shift_left",
"Prims.op_Equality",
"FStar.UInt32.__uint_to_t",
"FStar.UInt8.__uint_to_t",
"Prims.bool",
"FStar.UInt8.shift_left",
"FStar.UInt8.n"
] | [] | false | false | false | false | false | let u8_shift_left (x: U8.t) (amount: U32.t{U32.v amount <= 8})
: Tot (y: U8.t{U8.v y == (U8.v x) `U.shift_left` (U32.v amount)}) =
| let y = if amount = 8ul then 0uy else x `U8.shift_left` amount in
y | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield8 | val set_bitfield8
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) lo hi (U8.v v)}) | val set_bitfield8
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) lo hi (U8.v v)}) | let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 70,
"end_line": 1184,
"start_col": 0,
"start_line": 1177
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt8.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 8} ->
v: FStar.UInt8.t{FStar.UInt8.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt8.t
{FStar.UInt8.v y == LowParse.BitFields.set_bitfield (FStar.UInt8.v x) lo hi (FStar.UInt8.v v)} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt8.v",
"Prims.pow2",
"Prims.op_Subtraction",
"Prims.op_Equality",
"Prims.unit",
"LowParse.BitFields.set_bitfield_empty",
"Prims.bool",
"LowParse.BitFields.set_bitfield_gen8",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield8
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{U8.v y == set_bitfield (U8.v x) lo hi (U8.v v)}) =
| if lo = hi
then
(set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x)
else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq_shift | val bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` (bitfield_mask tot lo hi) == v `U.shift_left` lo) | val bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` (bitfield_mask tot lo hi) == v `U.shift_left` lo) | let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g () | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 30,
"end_line": 610,
"start_col": 0,
"start_line": 570
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt.uint_t tot ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= tot} ->
v: LowParse.BitFields.ubitfield tot (hi - lo)
-> FStar.Pervasives.Lemma
(ensures
LowParse.BitFields.get_bitfield x lo hi == v <==>
FStar.UInt.logand x (LowParse.BitFields.bitfield_mask tot lo hi) == FStar.UInt.shift_left v lo
) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.ubitfield",
"Prims.op_Subtraction",
"FStar.Classical.move_requires",
"Prims.unit",
"Prims.eq2",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowParse.BitFields.eq_nth",
"Prims.op_LessThan",
"LowParse.BitFields.nth_le_pow2_m",
"Prims.bool",
"LowParse.BitFields.nth_get_bitfield",
"LowParse.BitFields.nth_shift_left",
"LowParse.BitFields.nth_bitfield_mask",
"LowParse.BitFields.nth_logand",
"LowParse.BitFields.bitfield_mask",
"Prims.op_Addition",
"FStar.UInt.shift_left",
"LowParse.BitFields.get_bitfield",
"FStar.UInt.logand",
"Prims.l_True",
"Prims.l_iff"
] | [] | false | false | true | false | false | let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= tot})
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` (bitfield_mask tot lo hi) == v `U.shift_left` lo) =
| let y = x `U.logand` (bitfield_mask tot lo hi) in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma (requires (y == w)) (ensures (z == v)) =
eq_nth z
v
(fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i then nth_le_pow2_m v (hi - lo) i else nth_shift_left v lo (i + lo))
in
let g () : Lemma (requires (z == v)) (ensures (y == w)) =
eq_nth y
w
(fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i then nth_get_bitfield x lo hi (i - lo))
in
Classical.move_requires f ();
Classical.move_requires g () | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield16 | val set_bitfield16
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) lo hi (U16.v v)}) | val set_bitfield16
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) lo hi (U16.v v)}) | let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 94,
"end_line": 1075,
"start_col": 0,
"start_line": 1071
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt16.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 16} ->
v: FStar.UInt16.t{FStar.UInt16.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt16.t
{ FStar.UInt16.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt16.v x) lo hi (FStar.UInt16.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt16.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt16.logor",
"FStar.UInt16.logand",
"LowParse.BitFields.not_bitfield_mask16",
"LowParse.BitFields.u16_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield16
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) lo hi (U16.v v)}) =
| (x `U16.logand` (not_bitfield_mask16 lo hi)) `U16.logor` (v `u16_shift_left` (U32.uint_to_t lo)) | false |
LowParse.BitFields.fst | LowParse.BitFields.uint16 | val uint16 : uint_t 16 U16.t | val uint16 : uint_t 16 U16.t | let uint16 : uint_t 16 U16.t = {
v = U16.v;
uint_to_t = U16.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen16 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen16 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield16 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield16 x lo hi z);
logor = (fun x y -> U16.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq16_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq16_rhs x lo hi z);
} | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 1254,
"start_col": 0,
"start_line": 1242
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v
inline_for_extraction
let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi
inline_for_extraction
let bitfield_eq8_rhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U8.v x) lo hi (U8.v v)
in
v `u8_shift_left` U32.uint_to_t lo
inline_for_extraction
noextract
let uint64 : uint_t 64 U64.t = {
v = U64.v;
uint_to_t = U64.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen64 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen64 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield64 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield64 x lo hi z);
logor = (fun x y -> U64.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq64_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq64_rhs x lo hi z);
}
let uint64_v_eq x = ()
let uint64_uint_to_t_eq x = ()
inline_for_extraction
noextract
let uint32 : uint_t 32 U32.t = {
v = U32.v;
uint_to_t = U32.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen32 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen32 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield32 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield32 x lo hi z);
logor = (fun x y -> U32.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq32_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq32_rhs x lo hi z);
}
let uint32_v_eq x = ()
let uint32_uint_to_t_eq x = ()
inline_for_extraction | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.BitFields.uint_t 16 FStar.UInt16.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.BitFields.Mkuint_t",
"FStar.UInt16.n",
"FStar.UInt16.t",
"FStar.UInt16.v",
"FStar.UInt16.uint_to_t",
"FStar.UInt.uint_t",
"Prims.unit",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.get_bitfield_gen16",
"Prims.eq2",
"LowParse.BitFields.get_bitfield",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.set_bitfield_gen16",
"LowParse.BitFields.set_bitfield",
"Prims.nat",
"LowParse.BitFields.get_bitfield16",
"LowParse.BitFields.set_bitfield16",
"FStar.UInt16.logor",
"FStar.UInt.logor",
"LowParse.BitFields.bitfield_eq16_lhs",
"LowParse.BitFields.bitfield_eq16_rhs",
"Prims.l_iff",
"LowParse.BitFields.uint_t"
] | [] | false | false | false | false | false | let uint16:uint_t 16 U16.t =
| {
v = U16.v;
uint_to_t = U16.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen16 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen16 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield16 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield16 x lo hi z);
logor = (fun x y -> U16.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq16_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq16_rhs x lo hi z)
} | false |
LowParse.BitFields.fst | LowParse.BitFields.uint8 | val uint8 : uint_t 8 U8.t | val uint8 : uint_t 8 U8.t | let uint8 : uint_t 8 U8.t = {
v = U8.v;
uint_to_t = U8.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen8 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen8 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield8 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield8 x lo hi z);
logor = (fun x y -> U8.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq8_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq8_rhs x lo hi z);
} | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 1,
"end_line": 1273,
"start_col": 0,
"start_line": 1261
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v
inline_for_extraction
let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi
inline_for_extraction
let bitfield_eq8_rhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U8.v x) lo hi (U8.v v)
in
v `u8_shift_left` U32.uint_to_t lo
inline_for_extraction
noextract
let uint64 : uint_t 64 U64.t = {
v = U64.v;
uint_to_t = U64.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen64 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen64 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield64 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield64 x lo hi z);
logor = (fun x y -> U64.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq64_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq64_rhs x lo hi z);
}
let uint64_v_eq x = ()
let uint64_uint_to_t_eq x = ()
inline_for_extraction
noextract
let uint32 : uint_t 32 U32.t = {
v = U32.v;
uint_to_t = U32.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen32 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen32 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield32 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield32 x lo hi z);
logor = (fun x y -> U32.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq32_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq32_rhs x lo hi z);
}
let uint32_v_eq x = ()
let uint32_uint_to_t_eq x = ()
inline_for_extraction
noextract
let uint16 : uint_t 16 U16.t = {
v = U16.v;
uint_to_t = U16.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen16 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen16 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield16 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield16 x lo hi z);
logor = (fun x y -> U16.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq16_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq16_rhs x lo hi z);
}
let uint16_v_eq x = ()
let uint16_uint_to_t_eq x = ()
inline_for_extraction | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.BitFields.uint_t 8 FStar.UInt8.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.BitFields.Mkuint_t",
"FStar.UInt8.n",
"FStar.UInt8.t",
"FStar.UInt8.v",
"FStar.UInt8.uint_to_t",
"FStar.UInt.uint_t",
"Prims.unit",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"LowParse.BitFields.get_bitfield_gen8",
"Prims.eq2",
"LowParse.BitFields.get_bitfield",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.set_bitfield_gen8",
"LowParse.BitFields.set_bitfield",
"Prims.nat",
"LowParse.BitFields.get_bitfield8",
"LowParse.BitFields.set_bitfield8",
"FStar.UInt8.logor",
"FStar.UInt.logor",
"LowParse.BitFields.bitfield_eq8_lhs",
"LowParse.BitFields.bitfield_eq8_rhs",
"Prims.l_iff",
"LowParse.BitFields.uint_t"
] | [] | false | false | false | false | false | let uint8:uint_t 8 U8.t =
| {
v = U8.v;
uint_to_t = U8.uint_to_t;
v_uint_to_t = (fun _ -> ());
uint_to_t_v = (fun _ -> ());
get_bitfield_gen = (fun x lo hi -> get_bitfield_gen8 x lo hi);
set_bitfield_gen = (fun x lo hi z -> set_bitfield_gen8 x lo hi z);
get_bitfield = (fun x lo hi -> get_bitfield8 x lo hi);
set_bitfield = (fun x lo hi z -> set_bitfield8 x lo hi z);
logor = (fun x y -> U8.logor x y);
bitfield_eq_lhs = (fun x lo hi -> bitfield_eq8_lhs x lo hi);
bitfield_eq_rhs = (fun x lo hi z -> bitfield_eq8_rhs x lo hi z)
} | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_gen8 | val get_bitfield_gen8 (x: U8.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t{U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi)}) | val get_bitfield_gen8 (x: U8.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t{U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi)}) | let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 1137,
"start_col": 0,
"start_line": 1130
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt8.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 8}
-> y:
FStar.UInt8.t
{ FStar.UInt8.v y ==
LowParse.BitFields.get_bitfield (FStar.UInt8.v x) (FStar.UInt32.v lo) (FStar.UInt32.v hi) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt8.shift_right",
"FStar.UInt32.add",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt8.shift_left",
"Prims.unit",
"LowParse.BitFields.get_bitfield_eq_2",
"FStar.UInt8.v",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt8.n",
"LowParse.BitFields.get_bitfield"
] | [] | false | false | false | false | false | let get_bitfield_gen8 (x: U8.t) (lo: U32.t) (hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t{U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi)}) =
| get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2 | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq16_rhs | val bitfield_eq16_rhs
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v}) | val bitfield_eq16_rhs
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v}) | let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 37,
"end_line": 1091,
"start_col": 0,
"start_line": 1084
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt16.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 16} ->
v: FStar.UInt16.t{FStar.UInt16.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt16.t
{ LowParse.BitFields.bitfield_eq16_lhs x lo hi == y <==>
LowParse.BitFields.get_bitfield16 x lo hi == v } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt16.v",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.u16_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_eq_shift",
"FStar.UInt16.n",
"Prims.l_iff",
"Prims.eq2",
"LowParse.BitFields.bitfield_eq16_lhs",
"LowParse.BitFields.get_bitfield16"
] | [] | false | false | false | false | false | let bitfield_eq16_rhs
(x: U16.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 16})
(v: U16.t{U16.v v < pow2 (hi - lo)})
: Tot (y: U16.t{bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v}) =
| [@@ inline_let ]let _ = bitfield_eq_shift (U16.v x) lo hi (U16.v v) in
v `u16_shift_left` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_gen16 | val set_bitfield_gen16
(x: U16.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t{U16.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v)}) | val set_bitfield_gen16
(x: U16.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t{U16.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v)}) | let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 159,
"end_line": 1108,
"start_col": 0,
"start_line": 1103
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt16.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 16} ->
v: FStar.UInt16.t{FStar.UInt16.v v < Prims.pow2 (FStar.UInt32.v hi - FStar.UInt32.v lo)}
-> y:
FStar.UInt16.t
{ FStar.UInt16.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt16.v x)
(FStar.UInt32.v lo)
(FStar.UInt32.v hi)
(FStar.UInt16.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt16.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"FStar.UInt16.v",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt16.logor",
"FStar.UInt16.logand",
"FStar.UInt16.lognot",
"FStar.UInt16.shift_left",
"FStar.UInt16.shift_right",
"FStar.UInt16.__uint_to_t",
"FStar.UInt32.sub",
"FStar.UInt32.__uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt16.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield_gen16
(x: U16.t)
(lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t{U16.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U16.t{U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v)}) =
| bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x
`U16.logand`
(U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo)))
`U16.shift_left`
lo)))
`U16.logor`
(v `U16.shift_left` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.bitfield_eq8_rhs | val bitfield_eq8_rhs
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v}) | val bitfield_eq8_rhs
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v}) | let bitfield_eq8_rhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U8.v x) lo hi (U8.v v)
in
v `u8_shift_left` U32.uint_to_t lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 36,
"end_line": 1200,
"start_col": 0,
"start_line": 1193
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16"
inline_for_extraction
let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo)
#pop-options
(* Instantiate to UInt16 *)
inline_for_extraction
let bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == bitfield_mask 16 lo hi }) =
if lo = hi
then 0us
else begin
bitfield_mask_eq_2 16 lo hi;
(U16.lognot 0us `U16.shift_right` (16ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U16.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u16_shift_right
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_right` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_right` amount
inline_for_extraction
let get_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) lo hi })
= (x `U16.logand` bitfield_mask16 lo hi) `u16_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask16 (lo: nat) (hi: nat { lo <= hi /\ hi <= 16 }) : Tot (x: U16.t { U16.v x == not_bitfield_mask 16 lo hi }) =
U16.lognot (bitfield_mask16 lo hi)
inline_for_extraction
let u16_shift_left
(x: U16.t)
(amount: U32.t { U32.v amount <= 16 })
: Tot (y: U16.t { U16.v y == U16.v x `U.shift_left` U32.v amount })
= if amount = 16ul then 0us else x `U16.shift_left` amount
inline_for_extraction
let set_bitfield16
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) lo hi (U16.v v) })
= (x `U16.logand` not_bitfield_mask16 lo hi) `U16.logor` (v `u16_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq16_lhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
: Tot U16.t
= x `U16.logand` bitfield_mask16 lo hi
inline_for_extraction
let bitfield_eq16_rhs
(x: U16.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 16})
(v: U16.t { U16.v v < pow2 (hi - lo) })
: Tot (y: U16.t { bitfield_eq16_lhs x lo hi == y <==> (get_bitfield16 x lo hi <: U16.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U16.v x) lo hi (U16.v v)
in
v `u16_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
: Tot (y: U16.t { U16.v y == get_bitfield (U16.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #16 (U16.v x) (U32.v lo) (U32.v hi);
(* avoid integer promotion again *)
let bf : U16.t = x `U16.shift_left` (16ul `U32.sub` hi) in
bf `U16.shift_right` ((16ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen16
(x: U16.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 16})
(v: U16.t { U16.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U16.t { U16.v y == set_bitfield (U16.v x) (U32.v lo) (U32.v hi) (U16.v v) })
= bitfield_mask_eq_2 16 (U32.v lo) (U32.v hi);
(x `U16.logand` U16.lognot (((U16.lognot 0us) `U16.shift_right` (16ul `U32.sub` (hi `U32.sub` lo))) `U16.shift_left` lo)) `U16.logor` (v `U16.shift_left` lo)
inline_for_extraction
let bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == bitfield_mask 8 lo hi }) =
if lo = hi
then 0uy
else begin
bitfield_mask_eq_2 8 lo hi;
(U8.lognot 0uy `U8.shift_right` (8ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U8.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u8_shift_right
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_right` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_right` amount
in
y
// inline_for_extraction // no, because of https://github.com/FStarLang/karamel/issues/102
let get_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #8 (U8.v x) (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op1 = x `U8.shift_left` (8ul `U32.sub` hi) in
let op2 = op1 `U8.shift_right` ((8ul `U32.sub` hi) `U32.add` lo) in
op2
// inline_for_extraction // no, same
let set_bitfield_gen8
(x: U8.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 8})
(v: U8.t { U8.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) (U32.v lo) (U32.v hi) (U8.v v) })
= bitfield_mask_eq_2 8 (U32.v lo) (U32.v hi);
(* NOTE: due to https://github.com/FStarLang/karamel/issues/102 I need to introduce explicit let-bindings here *)
let op0 = (U8.lognot 0uy) in
let op1 = op0 `U8.shift_right` (8ul `U32.sub` (hi `U32.sub` lo)) in
let op2 = op1 `U8.shift_left` lo in
let op3 = U8.lognot op2 in
let op4 = x `U8.logand` op3 in
let op5 = v `U8.shift_left` lo in
let op6 = op4 `U8.logor` op5 in
op6
inline_for_extraction
let get_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot (y: U8.t { U8.v y == get_bitfield (U8.v x) lo hi })
= if lo = hi then 0uy else
get_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi)
inline_for_extraction
let not_bitfield_mask8 (lo: nat) (hi: nat { lo <= hi /\ hi <= 8 }) : Tot (x: U8.t { U8.v x == not_bitfield_mask 8 lo hi }) =
U8.lognot (bitfield_mask8 lo hi)
inline_for_extraction
let u8_shift_left
(x: U8.t)
(amount: U32.t { U32.v amount <= 8 })
: Tot (y: U8.t { U8.v y == U8.v x `U.shift_left` U32.v amount })
= let y =
if amount = 8ul then 0uy else x `U8.shift_left` amount
in
y
inline_for_extraction
let set_bitfield8
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
(v: U8.t { U8.v v < pow2 (hi - lo) })
: Tot (y: U8.t { U8.v y == set_bitfield (U8.v x) lo hi (U8.v v) })
= if lo = hi then begin
set_bitfield_empty #8 (U8.v x) lo (U8.v v);
x
end else set_bitfield_gen8 x (U32.uint_to_t lo) (U32.uint_to_t hi) v
inline_for_extraction
let bitfield_eq8_lhs
(x: U8.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 8})
: Tot U8.t
= x `U8.logand` bitfield_mask8 lo hi | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 32,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt8.t ->
lo: Prims.nat ->
hi: Prims.nat{lo <= hi /\ hi <= 8} ->
v: FStar.UInt8.t{FStar.UInt8.v v < Prims.pow2 (hi - lo)}
-> y:
FStar.UInt8.t
{ LowParse.BitFields.bitfield_eq8_lhs x lo hi == y <==>
LowParse.BitFields.get_bitfield8 x lo hi == v } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt8.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"FStar.UInt8.v",
"Prims.pow2",
"Prims.op_Subtraction",
"LowParse.BitFields.u8_shift_left",
"FStar.UInt32.uint_to_t",
"Prims.unit",
"LowParse.BitFields.bitfield_eq_shift",
"FStar.UInt8.n",
"Prims.l_iff",
"Prims.eq2",
"LowParse.BitFields.bitfield_eq8_lhs",
"LowParse.BitFields.get_bitfield8"
] | [] | false | false | false | false | false | let bitfield_eq8_rhs
(x: U8.t)
(lo: nat)
(hi: nat{lo <= hi /\ hi <= 8})
(v: U8.t{U8.v v < pow2 (hi - lo)})
: Tot (y: U8.t{bitfield_eq8_lhs x lo hi == y <==> (get_bitfield8 x lo hi <: U8.t) == v}) =
| [@@ inline_let ]let _ = bitfield_eq_shift (U8.v x) lo hi (U8.v v) in
v `u8_shift_left` (U32.uint_to_t lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.set_bitfield_gen32 | val set_bitfield_gen32
(x lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t{U32.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v)}) | val set_bitfield_gen32
(x lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t{U32.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v)}) | let set_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t { U32.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v) })
= bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x `U32.logand` U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo))) `U32.shift_left` lo)) `U32.logor` (v `U32.shift_left` lo) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 159,
"end_line": 1031,
"start_col": 0,
"start_line": 1026
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16"
let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
)
#pop-options
let get_bitfield_partition_2
(#tot: pos)
(mid: nat { mid <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x 0 mid == get_bitfield y 0 mid /\
get_bitfield x mid tot == get_bitfield y mid tot
))
(ensures (
x == y
))
= get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y
let rec get_bitfield_partition'
(#tot: pos)
(x y: U.uint_t tot)
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot })
(l: list nat)
: Lemma
(requires (get_bitfield_partition_prop x y lo hi l))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi))
(decreases l)
= match l with
| [] -> ()
| mi :: q ->
get_bitfield_partition' x y mi hi q;
get_bitfield_partition_2_gen lo mi hi x y
let get_bitfield_partition = get_bitfield_partition'
let rec nth_size (n1: nat) (n2: nat { n1 <= n2 }) (x: U.uint_t n1) (i: nat { i < n2 }) : Lemma
(x < pow2 n2 /\ nth #n2 x i == (i < n1 && nth #n1 x i))
= M.pow2_le_compat n2 n1;
if i < n1
then begin
if i = 0
then ()
else nth_size (n1 - 1) (n2 - 1) (x / 2) (i - 1)
end else nth_le_pow2_m #n2 x n1 i
let get_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
: Lemma
(x < pow2 tot2 /\ (get_bitfield #tot1 x lo hi <: nat) == (get_bitfield #tot2 x lo hi <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (get_bitfield #tot1 x lo hi) (get_bitfield #tot2 x lo hi) (fun i ->
let y1 = nth #tot2 (get_bitfield #tot1 x lo hi) i in
let y2 = nth #tot2 (get_bitfield #tot2 x lo hi) i in
nth_get_bitfield #tot2 x lo hi i;
assert (y2 == (i < hi - lo && nth #tot2 x (i + lo)));
nth_size tot1 tot2 (get_bitfield #tot1 x lo hi) i;
assert (y1 == (i < tot1 && nth #tot1 (get_bitfield #tot1 x lo hi) i));
if i < tot1
then begin
nth_get_bitfield #tot1 x lo hi i;
assert (y1 == (i < hi - lo && nth #tot1 x (i + lo)));
if i < hi - lo
then nth_size tot1 tot2 x (i + lo)
end
)
let set_bitfield_size
(tot1 tot2: pos)
(x: nat { x < pow2 tot1 /\ tot1 <= tot2 })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= tot1 })
(v: ubitfield tot1 (hi - lo))
: Lemma
(x < pow2 tot2 /\ v < pow2 tot2 /\ (set_bitfield #tot1 x lo hi v <: nat) == (set_bitfield #tot2 x lo hi v <: nat))
= M.pow2_le_compat tot2 tot1;
eq_nth #tot2 (set_bitfield #tot1 x lo hi v) (set_bitfield #tot2 x lo hi v) (fun i ->
let y1 = nth #tot2 (set_bitfield #tot1 x lo hi v) i in
let y2 = nth #tot2 (set_bitfield #tot2 x lo hi v) i in
nth_set_bitfield #tot2 x lo hi v i;
nth_size tot1 tot2 (set_bitfield #tot1 x lo hi v) i;
nth_size tot1 tot2 x i;
if i < tot1
then begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
)
let set_bitfield_bound
(#tot: pos)
(x: U.uint_t tot)
(bound: nat { bound <= tot /\ x < pow2 bound })
(lo: nat)
(hi: nat { lo <= hi /\ hi <= bound })
(v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v < pow2 bound)
= if bound = 0
then set_bitfield_empty x lo v
else begin
M.pow2_le_compat tot bound;
M.pow2_le_compat bound (hi - lo);
set_bitfield_size bound tot x lo hi v
end
#push-options "--z3rlimit 64 --z3cliopt smt.arith.nl=false --fuel 0 --ifuel 0"
let set_bitfield_set_bitfield_get_bitfield
#tot x lo hi lo' hi' v'
= set_bitfield_bound (get_bitfield x lo hi) (hi - lo) lo' hi' v' ;
let x1 = set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') in
let x2 = set_bitfield x (lo + lo') (lo + hi') v' in
eq_nth x1 x2 (fun i ->
nth_set_bitfield x lo hi (set_bitfield (get_bitfield x lo hi) lo' hi' v') i;
nth_set_bitfield x (lo + lo') (lo + hi') v' i ;
if lo <= i && i < hi
then begin
assert (nth x1 i == nth (set_bitfield (get_bitfield x lo hi) lo' hi' v') (i - lo));
nth_set_bitfield (get_bitfield x lo hi) lo' hi' v' (i - lo);
if lo' <= i - lo && i - lo < hi'
then begin
()
end
else begin
assert (nth x2 i == nth x i);
assert (nth x1 i == nth (get_bitfield x lo hi) (i - lo));
nth_get_bitfield x lo hi (i - lo);
assert (i - lo + lo == i)
end
end
)
#pop-options
let mod_1 (x: int) : Lemma
(x % 1 == 0)
= ()
let div_1 (x: int) : Lemma
(x / 1 == x)
= ()
let get_bitfield_eq (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(get_bitfield x lo hi == (x / pow2 lo) % pow2 (hi - lo))
= if hi - lo = 0
then begin
assert (hi == lo);
assert_norm (pow2 0 == 1);
mod_1 (x / pow2 lo);
get_bitfield_empty #tot x lo
end else if hi - lo = tot
then begin
assert (hi == tot);
assert (lo == 0);
assert_norm (pow2 0 == 1);
div_1 x;
M.small_mod x (pow2 tot);
get_bitfield_full #tot x
end else begin
assert (hi - lo < tot);
U.shift_right_logand_lemma #tot x (bitfield_mask tot lo hi) lo;
U.shift_right_value_lemma #tot (bitfield_mask tot lo hi) lo;
M.multiple_division_lemma (pow2 (hi - lo) - 1) (pow2 lo);
U.logand_mask #tot (U.shift_right x lo) (hi - lo);
U.shift_right_value_lemma #tot x lo
end
let pow2_m_minus_one_eq
(n: nat)
(m: nat)
: Lemma
(requires (m <= n))
(ensures (
(pow2 n - 1) / pow2 m == pow2 (n - m) - 1
))
= M.pow2_le_compat n m;
M.pow2_plus (n - m) m;
M.division_definition (pow2 n - 1) (pow2 m) (pow2 (n - m) - 1)
let get_bitfield_eq_2
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == (x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo))
= eq_nth (get_bitfield x lo hi) ((x `U.shift_left` (tot - hi)) `U.shift_right` (tot - hi + lo)) (fun i ->
nth_get_bitfield x lo hi i;
nth_shift_right (x `U.shift_left` (tot - hi)) (tot - hi + lo) i;
let j = i + (tot - hi + lo) in
if j < tot
then nth_shift_left x (tot - hi) j
)
#restart-solver
let bitfield_mask_eq_2 (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
bitfield_mask tot lo hi == U.shift_left #tot (U.lognot 0 `U.shift_right` (tot - (hi - lo))) lo
)
= bitfield_mask_eq tot lo hi;
pow2_m_minus_one_eq tot (tot - (hi - lo));
U.lemma_lognot_value_mod #tot 0;
U.shift_right_value_lemma #tot (pow2 tot - 1) (tot - (hi - lo))
let set_bitfield_eq
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(set_bitfield x lo hi v == (x `U.logand` U.lognot ((U.lognot 0 `U.shift_right` (tot - (hi - lo))) `U.shift_left` lo)) `U.logor` (v `U.shift_left` lo))
= bitfield_mask_eq_2 tot lo hi
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U16 = FStar.UInt16
module U8 = FStar.UInt8
(* Instantiate to UInt64 *)
#push-options "--z3rlimit 32"
inline_for_extraction
let bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == bitfield_mask 64 lo hi }) =
if lo = hi
then 0uL
else begin
bitfield_mask_eq_2 64 lo hi;
(U64.lognot 0uL `U64.shift_right` (64ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U64.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u64_shift_right
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_right` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_right` amount
inline_for_extraction
let get_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) lo hi })
= (x `U64.logand` bitfield_mask64 lo hi) `u64_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask64 (lo: nat) (hi: nat { lo <= hi /\ hi <= 64 }) : Tot (x: U64.t { U64.v x == not_bitfield_mask 64 lo hi }) =
U64.lognot (bitfield_mask64 lo hi)
inline_for_extraction
let u64_shift_left
(x: U64.t)
(amount: U32.t { U32.v amount <= 64 })
: Tot (y: U64.t { U64.v y == U64.v x `U.shift_left` U32.v amount })
= if amount = 64ul then 0uL else x `U64.shift_left` amount
inline_for_extraction
let set_bitfield64
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) lo hi (U64.v v) })
= (x `U64.logand` not_bitfield_mask64 lo hi) `U64.logor` (v `u64_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq64_lhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
: Tot U64.t
= x `U64.logand` bitfield_mask64 lo hi
#push-options "--z3rlimit 16"
inline_for_extraction
let bitfield_eq64_rhs
(x: U64.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 64})
(v: U64.t { U64.v v < pow2 (hi - lo) })
: Tot (y: U64.t { bitfield_eq64_lhs x lo hi == y <==> (get_bitfield64 x lo hi <: U64.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U64.v x) lo hi (U64.v v)
in
v `u64_shift_left` U32.uint_to_t lo
#pop-options
inline_for_extraction
let get_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
: Tot (y: U64.t { U64.v y == get_bitfield (U64.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #64 (U64.v x) (U32.v lo) (U32.v hi);
(x `U64.shift_left` (64ul `U32.sub` hi)) `U64.shift_right` ((64ul `U32.sub` hi) `U32.add` lo)
inline_for_extraction
let set_bitfield_gen64
(x: U64.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 64})
(v: U64.t { U64.v v < pow2 (U32.v hi - U32.v lo) })
: Tot (y: U64.t { U64.v y == set_bitfield (U64.v x) (U32.v lo) (U32.v hi) (U64.v v) })
= bitfield_mask_eq_2 64 (U32.v lo) (U32.v hi);
(x `U64.logand` U64.lognot (((U64.lognot 0uL) `U64.shift_right` (64ul `U32.sub` (hi `U32.sub` lo))) `U64.shift_left` lo)) `U64.logor` (v `U64.shift_left` lo)
(* Instantiate to UInt32 *)
inline_for_extraction
let bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == bitfield_mask 32 lo hi }) =
if lo = hi
then 0ul
else begin
bitfield_mask_eq_2 32 lo hi;
(U32.lognot 0ul `U32.shift_right` (32ul `U32.sub` (U32.uint_to_t (hi - lo)))) `U32.shift_left` U32.uint_to_t lo
end
inline_for_extraction
let u32_shift_right
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_right` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_right` amount
inline_for_extraction
let get_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) lo hi })
= (x `U32.logand` bitfield_mask32 lo hi) `u32_shift_right` (U32.uint_to_t lo)
inline_for_extraction
let not_bitfield_mask32 (lo: nat) (hi: nat { lo <= hi /\ hi <= 32 }) : Tot (x: U32.t { U32.v x == not_bitfield_mask 32 lo hi }) =
U32.lognot (bitfield_mask32 lo hi)
inline_for_extraction
let u32_shift_left
(x: U32.t)
(amount: U32.t { U32.v amount <= 32 })
: Tot (y: U32.t { U32.v y == U32.v x `U.shift_left` U32.v amount })
= if amount = 32ul then 0ul else x `U32.shift_left` amount
inline_for_extraction
let set_bitfield32
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { U32.v y == set_bitfield (U32.v x) lo hi (U32.v v) })
= (x `U32.logand` not_bitfield_mask32 lo hi) `U32.logor` (v `u32_shift_left` U32.uint_to_t lo)
inline_for_extraction
let bitfield_eq32_lhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
: Tot U32.t
= x `U32.logand` bitfield_mask32 lo hi
inline_for_extraction
let bitfield_eq32_rhs
(x: U32.t) (lo: nat) (hi: nat {lo <= hi /\ hi <= 32})
(v: U32.t { U32.v v < pow2 (hi - lo) })
: Tot (y: U32.t { bitfield_eq32_lhs x lo hi == y <==> (get_bitfield32 x lo hi <: U32.t) == v })
= [@inline_let] let _ =
bitfield_eq_shift (U32.v x) lo hi (U32.v v)
in
v `u32_shift_left` U32.uint_to_t lo
inline_for_extraction
let get_bitfield_gen32
(x: U32.t) (lo: U32.t) (hi: U32.t {U32.v lo < U32.v hi /\ U32.v hi <= 32})
: Tot (y: U32.t { U32.v y == get_bitfield (U32.v x) (U32.v lo) (U32.v hi) })
= get_bitfield_eq_2 #32 (U32.v x) (U32.v lo) (U32.v hi);
(x `U32.shift_left` (32ul `U32.sub` hi)) `U32.shift_right` ((32ul `U32.sub` hi) `U32.add` lo)
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x: FStar.UInt32.t ->
lo: FStar.UInt32.t ->
hi: FStar.UInt32.t{FStar.UInt32.v lo < FStar.UInt32.v hi /\ FStar.UInt32.v hi <= 32} ->
v: FStar.UInt32.t{FStar.UInt32.v v < Prims.pow2 (FStar.UInt32.v hi - FStar.UInt32.v lo)}
-> y:
FStar.UInt32.t
{ FStar.UInt32.v y ==
LowParse.BitFields.set_bitfield (FStar.UInt32.v x)
(FStar.UInt32.v lo)
(FStar.UInt32.v hi)
(FStar.UInt32.v v) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.op_LessThanOrEqual",
"Prims.pow2",
"Prims.op_Subtraction",
"FStar.UInt32.logor",
"FStar.UInt32.logand",
"FStar.UInt32.lognot",
"FStar.UInt32.shift_left",
"FStar.UInt32.shift_right",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.sub",
"Prims.unit",
"LowParse.BitFields.bitfield_mask_eq_2",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt32.n",
"LowParse.BitFields.set_bitfield"
] | [] | false | false | false | false | false | let set_bitfield_gen32
(x lo: U32.t)
(hi: U32.t{U32.v lo < U32.v hi /\ U32.v hi <= 32})
(v: U32.t{U32.v v < pow2 (U32.v hi - U32.v lo)})
: Tot (y: U32.t{U32.v y == set_bitfield (U32.v x) (U32.v lo) (U32.v hi) (U32.v v)}) =
| bitfield_mask_eq_2 32 (U32.v lo) (U32.v hi);
(x
`U32.logand`
(U32.lognot (((U32.lognot 0ul) `U32.shift_right` (32ul `U32.sub` (hi `U32.sub` lo)))
`U32.shift_left`
lo)))
`U32.logor`
(v `U32.shift_left` lo) | false |
LowParse.BitFields.fst | LowParse.BitFields.get_bitfield_partition_2_gen | val get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
)) | val get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
)) | let get_bitfield_partition_2_gen
(#tot: pos)
(lo: nat)
(mi: nat)
(hi: nat { lo <= mi /\ mi <= hi /\ hi <= tot })
(x y: U.uint_t tot)
: Lemma
(requires (
get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi
))
(ensures (
get_bitfield x lo hi == get_bitfield y lo hi
))
= eq_nth (get_bitfield x lo hi) (get_bitfield y lo hi) (fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then begin
if i < mi - lo
then begin
nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i
end else begin
nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi)
end
end
) | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 3,
"end_line": 660,
"start_col": 0,
"start_line": 630
} | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction
let bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Tot (U.uint_t tot) =
[@inline_let] let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo)
let bitfield_mask_eq (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) : Lemma
(
0 <= pow2 (hi - lo) - 1 /\
pow2 (hi - lo) - 1 < pow2 tot /\
bitfield_mask tot lo hi == U.shift_left #tot (pow2 (hi - lo) - 1) lo
)
=
M.pow2_le_compat tot (hi - lo);
U.shift_left_value_lemma #tot (pow2 (hi - lo) - 1) lo;
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo;
M.lemma_mult_nat (pow2 (hi - lo) - 1) (pow2 lo);
M.lemma_mult_lt' (pow2 lo) (pow2 (hi - lo) - 1) (pow2 (hi - lo));
M.swap_mul (pow2 (hi - lo)) (pow2 lo);
M.swap_mul (pow2 (hi - lo) - 1) (pow2 lo);
M.modulo_lemma ((pow2 (hi - lo) - 1) * pow2 lo) (pow2 tot)
(* Cf. U.index_to_vec_ones; WHY WHY WHY is it private? *)
val nth_pow2_minus_one' : #n:pos -> m:nat{m <= n} -> i:nat{i < n} ->
Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(i < n - m ==> U.nth #n (pow2 m - 1) i == false) /\
(n - m <= i ==> U.nth #n (pow2 m - 1) i == true)))
let rec nth_pow2_minus_one' #n m i =
let a = pow2 m - 1 in
M.pow2_le_compat n m;
if m = 0 then U.one_to_vec_lemma #n i
else if m = n then U.ones_to_vec_lemma #n i
else if i = n - 1 then ()
else nth_pow2_minus_one' #(n - 1) (m - 1) i
(* Rephrasing with a more natural nth *)
let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i)
let eq_nth
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(f: (
(i: nat { i < n }) ->
Lemma
(nth a i == nth b i)
))
: Lemma
(a == b)
= let g
(i: nat { i < n })
: Lemma
(U.nth a i == U.nth b i)
= f (n - 1 - i)
in
Classical.forall_intro g;
U.nth_lemma a b
let nth_pow2_minus_one
(#n:pos)
(m:nat{m <= n})
(i:nat{i < n})
: Lemma (requires True)
(ensures (pow2 m <= pow2 n /\
(nth #n (pow2 m - 1) i == (i < m))))
= nth_pow2_minus_one' #n m (n - 1 - i)
let nth_shift_left
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_left a s) i == (s <= i && nth a (i - s)))
= ()
let nth_shift_right
(#n: pos)
(a: U.uint_t n)
(s: nat)
(i: nat {i < n})
: Lemma
(nth (U.shift_right a s) i == (i + s < n && nth a (i + s)))
= ()
let nth_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (bitfield_mask tot lo hi) i == (lo <= i && i < hi))
= bitfield_mask_eq tot lo hi;
nth_shift_left #tot (pow2 (hi - lo) - 1) lo i;
if i < lo
then ()
else begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end
let get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) =
(x `U.logand` bitfield_mask tot lo hi) `U.shift_right` lo
let nth_logand
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logand` b) i == (nth a i && nth b i))
= ()
let nth_logor
(#n: pos)
(a b: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (a `U.logor` b) i == (nth a i || nth b i))
= ()
let nth_lognot
(#n: pos)
(a: U.uint_t n)
(i: nat {i < n})
: Lemma
(nth (U.lognot a) i == not (nth a i))
= ()
let nth_get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield_raw x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_shift_right (x `U.logand` bitfield_mask tot lo hi) lo i;
if i + lo < tot
then begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
()
let get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) : Lemma
(let y = get_bitfield_raw x lo hi in
pow2 (hi - lo) - 1 < pow2 tot /\
y == y `U.logand` (pow2 (hi - lo) - 1)
)
= nth_pow2_minus_one #tot (hi - lo) 0;
let y = get_bitfield_raw x lo hi in
eq_nth y (y `U.logand` (pow2 (hi - lo) - 1)) (fun i ->
nth_get_bitfield_raw x lo hi i;
nth_pow2_minus_one #tot (hi - lo) i;
nth_logand y (pow2 (hi - lo) - 1) i
)
#push-options "--z3rlimit 16"
let logand_mask
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
: Lemma
(pow2 m <= pow2 n /\
U.logand a (pow2 m - 1) == a % pow2 m)
= M.pow2_le_compat n m;
if m = 0
then begin
U.logand_lemma_1 a
end else if m = n
then begin
U.logand_lemma_2 a;
M.modulo_lemma a (pow2 m)
end else begin
U.logand_mask a m
end
#pop-options
let nth_le_pow2_m
(#n: pos)
(a: U.uint_t n)
(m: nat {m <= n})
(i: nat {i < n})
: Lemma
(requires (a < pow2 m /\ m <= i))
(ensures (nth a i == false))
= logand_mask a m;
M.modulo_lemma a (pow2 m);
nth_pow2_minus_one #n m i;
nth_logand a (pow2 m - 1) i
let get_bitfield_raw_bounded
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield_raw x lo hi < pow2 (hi - lo))
= get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one x lo hi;
logand_mask (get_bitfield_raw x lo hi) (hi - lo);
M.lemma_mod_lt x (pow2 (hi - lo))
let get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo))
= get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi
let nth_get_bitfield (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot}) (i: nat {i < tot}) : Lemma
(nth (get_bitfield x lo hi) i == (i < hi - lo && nth x (i + lo)))
= nth_get_bitfield_raw x lo hi i
let get_bitfield_logor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logor` y) lo hi == get_bitfield x lo hi `U.logor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logor` y) lo hi) (get_bitfield x lo hi `U.logor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
let get_bitfield_logxor
(#tot: pos) (x y: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Lemma
(get_bitfield (x `U.logxor` y) lo hi == get_bitfield x lo hi `U.logxor` get_bitfield y lo hi)
= eq_nth (get_bitfield (x `U.logxor` y) lo hi) (get_bitfield x lo hi `U.logxor` get_bitfield y lo hi) (fun i ->
nth_get_bitfield (x `U.logxor` y) lo hi i;
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i
)
#push-options "--z3rlimit 16"
let logor_disjoint
(#n: pos)
(a: U.uint_t n)
(b: U.uint_t n)
(m: nat {m <= n})
: Lemma
(requires (
a % pow2 m == 0 /\
b < pow2 m
))
(ensures (U.logor #n a b == a + b))
= if m = 0
then
U.logor_lemma_1 a
else if m = n
then begin
M.modulo_lemma a (pow2 n);
U.logor_commutative a b;
U.logor_lemma_1 b
end else
U.logor_disjoint a b m
#pop-options
let bitfield_mask_mod_pow2_lo
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (m: nat {m <= lo})
: Lemma
(bitfield_mask tot lo hi % pow2 m == 0)
= M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo
let bitfield_mask_lt_pow2_hi
(tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(bitfield_mask tot lo hi < pow2 hi)
=
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
inline_for_extraction
let not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Tot (x: U.uint_t tot {x == U.lognot (bitfield_mask tot lo hi)})
= [@inline_let]
let a = bitfield_mask tot hi tot in
[@inline_let]
let b = bitfield_mask tot 0 lo in
[@inline_let]
let _ =
bitfield_mask_mod_pow2_lo tot hi tot lo;
bitfield_mask_lt_pow2_hi tot 0 lo;
logor_disjoint a b lo;
eq_nth (a `U.logor` b) (U.lognot (bitfield_mask tot lo hi)) (fun i ->
nth_bitfield_mask tot hi tot i;
nth_bitfield_mask tot 0 lo i;
nth_bitfield_mask tot lo hi i
)
in
normalize_term (a + b)
let nth_not_bitfield_mask (tot: pos) (lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (i: nat {i < tot}) : Lemma
(nth (not_bitfield_mask tot lo hi) i == (i < lo || hi <= i))
= nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i
let set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Tot (U.uint_t tot)
= (x `U.logand` not_bitfield_mask tot lo hi) `U.logor` (v `U.shift_left` lo)
let nth_set_bitfield
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(i: nat {i < tot})
: Lemma
(nth (set_bitfield x lo hi v) i == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
=
let y = nth (set_bitfield x lo hi v) i in
nth_logor (x `U.logand` not_bitfield_mask tot lo hi) (v `U.shift_left` lo) i;
assert (y == (nth (x `U.logand` not_bitfield_mask tot lo hi) i || nth (v `U.shift_left` lo) i));
nth_logand x (not_bitfield_mask tot lo hi) i;
assert (y == ((nth x i && nth (not_bitfield_mask tot lo hi) i) || nth (v `U.shift_left` lo) i));
nth_not_bitfield_mask tot lo hi i;
assert (y == ((nth x i && (i < lo || hi <= i)) || nth (v `U.shift_left` lo) i));
nth_shift_left v lo i;
assert (y == ((nth x i && (i < lo || hi <= i)) || (lo <= i && nth v (i - lo))));
if (lo <= i && i < hi) then
assert (y == nth v (i - lo))
else if (i < hi)
then
assert (y == nth x i)
else begin
nth_le_pow2_m v (hi - lo) (i - lo);
assert (y == nth x i)
end;
assert (y == (if lo <= i && i < hi then nth v (i - lo) else nth x i))
#push-options "--z3rlimit 32"
let get_bitfield_set_bitfield_same
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield (set_bitfield x lo hi v) lo hi == v)
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo hi) v (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo hi i;
if i < hi - lo
then begin
nth_set_bitfield x lo hi v (i + lo)
end else
nth_le_pow2_m v (hi - lo) i
)
#pop-options
let get_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot })
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (get_bitfield (set_bitfield x lo hi v) lo' hi' == get_bitfield x lo' hi'))
= eq_nth (get_bitfield (set_bitfield x lo hi v) lo' hi') (get_bitfield x lo' hi') (fun i ->
nth_get_bitfield (set_bitfield x lo hi v) lo' hi' i;
nth_get_bitfield x lo' hi' i;
if i < hi' - lo'
then begin
nth_set_bitfield x lo hi v (i + lo')
end
)
let set_bitfield_set_bitfield_same_gen
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= lo /\ hi <= hi' /\ hi' <= tot })
(v' : ubitfield tot (hi' - lo'))
: Lemma
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield x lo' hi' v'))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield x lo' hi' v') (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_set_bitfield_other
(#tot: pos) (x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot }) (v: ubitfield tot (hi - lo))
(lo' : nat) (hi' : nat { lo' <= hi' /\ hi' <= tot }) (v' : ubitfield tot (hi' - lo'))
: Lemma
(requires (hi' <= lo \/ hi <= lo'))
(ensures (set_bitfield (set_bitfield x lo hi v) lo' hi' v' == set_bitfield (set_bitfield x lo' hi' v') lo hi v))
= eq_nth (set_bitfield (set_bitfield x lo hi v) lo' hi' v') (set_bitfield (set_bitfield x lo' hi' v') lo hi v) (fun i ->
nth_set_bitfield (set_bitfield x lo hi v) lo' hi' v' i;
nth_set_bitfield x lo hi v i;
nth_set_bitfield (set_bitfield x lo' hi' v') lo hi v i;
nth_set_bitfield x lo' hi' v' i
)
let set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y)
= eq_nth (set_bitfield x 0 tot y) y (fun i ->
nth_set_bitfield x 0 tot y i
)
let set_bitfield_empty
(#tot: pos) (x: U.uint_t tot) (i: nat { i <= tot }) (y: ubitfield tot 0)
: Lemma
(set_bitfield x i i y == x)
= eq_nth (set_bitfield x i i y) x (fun j ->
nth_set_bitfield x i i y j
)
let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i
let get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0)
= eq_nth (get_bitfield #tot 0 lo hi) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield #tot 0 lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
let get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x)
= eq_nth (get_bitfield x 0 tot) x (fun i ->
nth_get_bitfield x 0 tot i
)
let get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0)
= eq_nth (get_bitfield x i i) 0 (fun j ->
nth_get_bitfield x i i j;
nth_zero tot j
)
let lt_pow2_get_bitfield_hi
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (x < pow2 mi))
(ensures (get_bitfield x mi tot == 0))
= if mi = 0
then get_bitfield_zero tot mi tot
else if mi < tot
then begin
M.modulo_lemma x (pow2 mi);
U.logand_mask x mi;
eq_nth (get_bitfield x mi tot) 0 (fun i ->
nth_zero tot i;
nth_get_bitfield x mi tot i;
nth_get_bitfield (x `U.logand` (pow2 mi - 1)) mi tot i;
nth_pow2_minus_one #tot mi i
)
end
let get_bitfield_hi_lt_pow2
(#tot: pos)
(x: U.uint_t tot)
(mi: nat {mi <= tot})
: Lemma
(requires (get_bitfield x mi tot == 0))
(ensures (x < pow2 mi))
= if mi = 0
then get_bitfield_full x
else if mi < tot
then begin
M.pow2_le_compat tot mi;
eq_nth x (x `U.logand` (pow2 mi - 1)) (fun i ->
nth_pow2_minus_one #tot mi i;
if mi <= i
then begin
nth_get_bitfield x mi tot (i - mi);
nth_zero tot (i - mi)
end
);
U.logand_mask x mi;
M.lemma_mod_lt x (pow2 mi)
end
let get_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat) (hi': nat { lo' <= hi' /\ hi' <= hi - lo })
: Lemma
(get_bitfield (get_bitfield x lo hi) lo' hi' == get_bitfield x (lo + lo') (lo + hi'))
= eq_nth (get_bitfield (get_bitfield x lo hi) lo' hi') (get_bitfield x (lo + lo') (lo + hi')) (fun i ->
nth_get_bitfield (get_bitfield x lo hi) lo' hi' i;
nth_get_bitfield x (lo + lo') (lo + hi') i ;
if i < hi' - lo'
then nth_get_bitfield x lo hi (i + lo')
)
#push-options "--z3rlimit_factor 2"
let get_bitfield_zero_inner
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(lo': nat { lo <= lo' }) (hi': nat { lo' <= hi' /\ hi' <= hi })
: Lemma
(ensures (get_bitfield x lo hi == 0 ==> get_bitfield x lo' hi' == 0))
= let f () : Lemma
(requires (get_bitfield x lo hi == 0))
(ensures (get_bitfield x lo' hi' == 0))
=
eq_nth (get_bitfield x lo' hi') 0 (fun i ->
nth_get_bitfield x lo' hi' i;
nth_zero tot i;
if (i < hi' - lo') then begin
nth_get_bitfield x lo hi (i + lo' - lo);
nth_zero tot (i + lo');
nth_zero tot (i + lo' - lo)
end
)
in
Classical.move_requires f ()
#pop-options
#push-options "--z3rlimit 32"
let bitfield_is_zero
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield x lo hi == 0 <==> x `U.logand` bitfield_mask tot lo hi == 0)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let f () : Lemma
(requires (y == 0))
(ensures (z == 0))
= eq_nth z 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if i < hi - lo
then nth_zero tot (i + lo)
)
in
let g () : Lemma
(requires (z == 0))
(ensures (y == 0))
= eq_nth y 0 (fun i ->
nth_zero tot i;
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
if lo <= i && i < hi
then begin
nth_get_bitfield x lo hi (i - lo);
nth_zero tot (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let bitfield_eq_shift
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
(v: ubitfield tot (hi - lo))
: Lemma
(get_bitfield x lo hi == v <==> x `U.logand` bitfield_mask tot lo hi == v `U.shift_left` lo)
=
let y = x `U.logand` bitfield_mask tot lo hi in
let z = get_bitfield x lo hi in
let w = v `U.shift_left` lo in
let f () : Lemma
(requires (y == w))
(ensures (z == v))
= eq_nth z v (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_get_bitfield x lo hi i;
if hi - lo <= i
then nth_le_pow2_m v (hi - lo) i
else nth_shift_left v lo (i + lo)
)
in
let g () : Lemma
(requires (z == v))
(ensures (y == w))
= eq_nth y w (fun i ->
nth_logand x (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i;
nth_shift_left v lo i;
if hi <= i
then
nth_le_pow2_m v (hi - lo) (i - lo)
else if lo <= i
then begin
nth_get_bitfield x lo hi (i - lo)
end
)
in
Classical.move_requires f ();
Classical.move_requires g ()
#pop-options
#push-options "--z3rlimit 16"
let set_bitfield_get_bitfield
(#tot: pos)
(x: U.uint_t tot)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(set_bitfield x lo hi (get_bitfield x lo hi) == x)
= eq_nth (set_bitfield x lo hi (get_bitfield x lo hi)) x (fun i ->
nth_set_bitfield x lo hi (get_bitfield x lo hi) i;
if lo <= i && i < hi
then nth_get_bitfield x lo hi (i - lo)
)
#pop-options
#push-options "--z3rlimit 16" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Math.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": true,
"source_file": "LowParse.BitFields.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowParse.Math",
"short_module": "M"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "U"
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 16,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
lo: Prims.nat ->
mi: Prims.nat ->
hi: Prims.nat{lo <= mi /\ mi <= hi /\ hi <= tot} ->
x: FStar.UInt.uint_t tot ->
y: FStar.UInt.uint_t tot
-> FStar.Pervasives.Lemma
(requires
LowParse.BitFields.get_bitfield x lo mi == LowParse.BitFields.get_bitfield y lo mi /\
LowParse.BitFields.get_bitfield x mi hi == LowParse.BitFields.get_bitfield y mi hi)
(ensures LowParse.BitFields.get_bitfield x lo hi == LowParse.BitFields.get_bitfield y lo hi) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt.uint_t",
"LowParse.BitFields.eq_nth",
"LowParse.BitFields.get_bitfield",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"LowParse.BitFields.nth_get_bitfield",
"Prims.unit",
"Prims.bool",
"Prims.op_Addition",
"LowParse.BitFields.nth",
"Prims.eq2",
"LowParse.BitFields.ubitfield",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let get_bitfield_partition_2_gen
(#tot: pos)
(lo mi: nat)
(hi: nat{lo <= mi /\ mi <= hi /\ hi <= tot})
(x y: U.uint_t tot)
: Lemma
(requires
(get_bitfield x lo mi == get_bitfield y lo mi /\
get_bitfield x mi hi == get_bitfield y mi hi))
(ensures (get_bitfield x lo hi == get_bitfield y lo hi)) =
| eq_nth (get_bitfield x lo hi)
(get_bitfield y lo hi)
(fun i ->
let a = nth (get_bitfield x lo hi) i in
let b = nth (get_bitfield y lo hi) i in
nth_get_bitfield x lo hi i;
nth_get_bitfield y lo hi i;
if i < hi - lo
then
if i < mi - lo
then
(nth_get_bitfield x lo mi i;
nth_get_bitfield y lo mi i)
else
(nth_get_bitfield x mi hi (i + lo - mi);
nth_get_bitfield y mi hi (i + lo - mi))) | false |
Vale.AES.X64.GCMdecryptOpt.fsti | Vale.AES.X64.GCMdecryptOpt.va_wp_Gcm_blocks_decrypt_stdcall | val va_wp_Gcm_blocks_decrypt_stdcall
(win: bool)
(alg: algorithm)
(auth_b: buffer128)
(auth_bytes auth_num: nat64)
(keys_b iv_b: buffer128)
(iv: supported_iv_LE)
(hkeys_b abytes_b in128x6_b out128x6_b: buffer128)
(len128x6_num: nat64)
(in128_b out128_b: buffer128)
(len128_num: nat64)
(inout_b: buffer128)
(cipher_num: nat64)
(scratch_b tag_b: buffer128)
(key: (seq nat32))
(va_s0: va_state)
(va_k: (va_state -> unit -> Type0))
: Type0 | val va_wp_Gcm_blocks_decrypt_stdcall
(win: bool)
(alg: algorithm)
(auth_b: buffer128)
(auth_bytes auth_num: nat64)
(keys_b iv_b: buffer128)
(iv: supported_iv_LE)
(hkeys_b abytes_b in128x6_b out128x6_b: buffer128)
(len128x6_num: nat64)
(in128_b out128_b: buffer128)
(len128_num: nat64)
(inout_b: buffer128)
(cipher_num: nat64)
(scratch_b tag_b: buffer128)
(key: (seq nat32))
(va_s0: va_state)
(va_k: (va_state -> unit -> Type0))
: Type0 | let va_wp_Gcm_blocks_decrypt_stdcall (win:bool) (alg:algorithm) (auth_b:buffer128)
(auth_bytes:nat64) (auth_num:nat64) (keys_b:buffer128) (iv_b:buffer128) (iv:supported_iv_LE)
(hkeys_b:buffer128) (abytes_b:buffer128) (in128x6_b:buffer128) (out128x6_b:buffer128)
(len128x6_num:nat64) (in128_b:buffer128) (out128_b:buffer128) (len128_num:nat64)
(inout_b:buffer128) (cipher_num:nat64) (scratch_b:buffer128) (tag_b:buffer128) (key:(seq nat32))
(va_s0:va_state) (va_k:(va_state -> unit -> Type0)) : Type0 =
(va_get_ok va_s0 /\ (let (auth_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
va_get_reg64 rRcx va_s0) (fun _ -> va_get_reg64 rRdi va_s0) in let
(auth_num_bytes:(va_int_range 0 18446744073709551615)) = va_if win (fun _ -> va_get_reg64 rRdx
va_s0) (fun _ -> va_get_reg64 rRsi va_s0) in let (auth_len:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> va_get_reg64 rR8 va_s0) (fun _ -> va_get_reg64
rRdx va_s0) in let (keys_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
va_get_reg64 rR9 va_s0) (fun _ -> va_get_reg64 rRcx va_s0) in let (iv_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 32 + 8 + 0) (va_get_stack va_s0)) (fun _ -> va_get_reg64 rR8 va_s0) in let
(xip:(va_int_range 0 18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 32 + 8 + 8) (va_get_stack va_s0)) (fun _ -> va_get_reg64 rR9 va_s0)
in let (abytes_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 16) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 0) (va_get_stack va_s0)) in let
(in128x6_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in let
(out128x6_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in
let (len128x6:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in
let (in128_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 48) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 32) (va_get_stack va_s0)) in
let (out128_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in
let (len128:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in
let (inout_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 72) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 56) (va_get_stack va_s0)) in
let (cipher_num_bytes:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in
let (scratch_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in
let (tag_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0)) (fun _
-> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in
sse_enabled /\ movbe_enabled /\ va_get_reg64 rRsp va_s0 == Vale.X64.Stack_i.init_rsp
(va_get_stack va_s0) /\ Vale.X64.Memory.is_initial_heap (va_get_mem_layout va_s0) (va_get_mem
va_s0) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 0)
(va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)
Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 32) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 56) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 0) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 8) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 16) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 48) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 72) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ auth_len == auth_num /\ auth_num_bytes ==
auth_bytes /\ len128x6 == len128x6_num /\ len128 == len128_num /\ cipher_num_bytes ==
cipher_num /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) auth_ptr auth_b auth_len
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
abytes_ptr abytes_b 1 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128
(va_get_mem va_s0) iv_ptr iv_b 1 (va_get_mem_layout va_s0) Public /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) in128x6_ptr in128x6_b len128x6
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
out128x6_ptr out128x6_b len128x6 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) in128_ptr in128_b len128 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) out128_ptr out128_b len128
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
inout_ptr inout_b 1 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128
(va_get_mem va_s0) scratch_ptr scratch_b 9 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) xip hkeys_b 8 (va_get_mem_layout va_s0)
Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) tag_ptr tag_b 1 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.buffer_disjoints128 tag_b ([auth_b; abytes_b; iv_b; in128x6_b;
out128x6_b; in128_b; out128_b; inout_b; scratch_b]) /\ Vale.X64.Decls.buffer_disjoints128 iv_b
([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; scratch_b;
hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 scratch_b ([keys_b; auth_b; abytes_b;
in128x6_b; out128x6_b; in128_b; out128_b; inout_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 inout_b ([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b;
in128_b; out128_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 auth_b ([keys_b; abytes_b;
hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 abytes_b ([keys_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 out128x6_b ([keys_b; auth_b; abytes_b; hkeys_b; in128_b;
inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128x6_b ([keys_b; auth_b; abytes_b; hkeys_b;
in128_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 out128_b ([keys_b; auth_b; abytes_b;
hkeys_b; in128x6_b; out128x6_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128_b
([keys_b; auth_b; abytes_b; hkeys_b; in128x6_b; out128x6_b; inout_b]) /\
(Vale.X64.Decls.buffers_disjoint128 in128x6_b out128x6_b \/ in128x6_b == out128x6_b) /\
(Vale.X64.Decls.buffers_disjoint128 in128_b out128_b \/ in128_b == out128_b) /\ auth_ptr + 16
`op_Multiply` auth_len < pow2_64 /\ in128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\
out128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\ in128_ptr + 16 `op_Multiply` len128 <
pow2_64 /\ out128_ptr + 16 `op_Multiply` len128 < pow2_64 /\ inout_ptr + 16 < pow2_64 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 auth_b == auth_len /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 abytes_b == 1 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128x6_b ==
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 out128x6_b /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b == Vale.X64.Decls.buffer_length
#Vale.X64.Memory.vuint128 out128_b /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128
in128x6_b == len128x6 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b ==
len128 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 inout_b == 1 /\
cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ xip + 32 < pow2_64 /\
Vale.X64.Memory.buffer_addr #Vale.X64.Memory.vuint128 keys_b (va_get_mem va_s0) + 128 < pow2_64
/\ len128x6 `op_Modulus` 6 == 0 /\ (len128x6 > 0 ==> len128x6 >= 6) /\ 12 + len128x6 + 6 <
pow2_32 /\ (va_mul_nat len128x6 (128 `op_Division` 8) + va_mul_nat len128 (128 `op_Division` 8)
<= cipher_num_bytes /\ cipher_num_bytes < va_mul_nat len128x6 (128 `op_Division` 8) +
va_mul_nat len128 (128 `op_Division` 8) + 128 `op_Division` 8) /\ (va_mul_nat auth_len (128
`op_Division` 8) <= auth_num_bytes /\ auth_num_bytes < va_mul_nat auth_len (128 `op_Division`
8) + 128 `op_Division` 8) /\ aes_reqs alg key (Vale.X64.Decls.buffer128_as_seq (va_get_mem
va_s0) keys_b) keys_b keys_ptr (va_get_mem va_s0) (va_get_mem_layout va_s0) /\
pclmulqdq_enabled /\ Vale.AES.OptPublic.hkeys_reqs_pub (Vale.X64.Decls.s128 (va_get_mem va_s0)
hkeys_b) (Vale.Def.Types_s.reverse_bytes_quad32 (Vale.AES.AES_s.aes_encrypt_LE alg key
(Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0))) /\ (let h_LE =
Vale.AES.AES_s.aes_encrypt_LE alg key (Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0)
in let iv_BE = Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in iv_BE ==
Vale.AES.GCM_s.compute_iv_BE h_LE iv)) /\ (forall (va_x_mem:vale_heap) (va_x_rax:nat64)
(va_x_rbx:nat64) (va_x_rcx:nat64) (va_x_rdx:nat64) (va_x_rdi:nat64) (va_x_rsi:nat64)
(va_x_rsp:nat64) (va_x_rbp:nat64) (va_x_r8:nat64) (va_x_r9:nat64) (va_x_r10:nat64)
(va_x_r11:nat64) (va_x_r12:nat64) (va_x_r13:nat64) (va_x_r14:nat64) (va_x_r15:nat64)
(va_x_xmm0:quad32) (va_x_xmm1:quad32) (va_x_xmm2:quad32) (va_x_xmm3:quad32) (va_x_xmm4:quad32)
(va_x_xmm5:quad32) (va_x_xmm6:quad32) (va_x_xmm7:quad32) (va_x_xmm8:quad32) (va_x_xmm9:quad32)
(va_x_xmm10:quad32) (va_x_xmm11:quad32) (va_x_xmm12:quad32) (va_x_xmm13:quad32)
(va_x_xmm14:quad32) (va_x_xmm15:quad32) (va_x_heap1:vale_heap) (va_x_heap2:vale_heap)
(va_x_heap3:vale_heap) (va_x_heap5:vale_heap) (va_x_heap6:vale_heap)
(va_x_memLayout:vale_heap_layout) (va_x_efl:Vale.X64.Flags.t) (va_x_stack:vale_stack)
(va_x_stackTaint:memtaint) . let va_sM = va_upd_stackTaint va_x_stackTaint (va_upd_stack
va_x_stack (va_upd_flags va_x_efl (va_upd_mem_layout va_x_memLayout (va_upd_mem_heaplet 6
va_x_heap6 (va_upd_mem_heaplet 5 va_x_heap5 (va_upd_mem_heaplet 3 va_x_heap3
(va_upd_mem_heaplet 2 va_x_heap2 (va_upd_mem_heaplet 1 va_x_heap1 (va_upd_xmm 15 va_x_xmm15
(va_upd_xmm 14 va_x_xmm14 (va_upd_xmm 13 va_x_xmm13 (va_upd_xmm 12 va_x_xmm12 (va_upd_xmm 11
va_x_xmm11 (va_upd_xmm 10 va_x_xmm10 (va_upd_xmm 9 va_x_xmm9 (va_upd_xmm 8 va_x_xmm8
(va_upd_xmm 7 va_x_xmm7 (va_upd_xmm 6 va_x_xmm6 (va_upd_xmm 5 va_x_xmm5 (va_upd_xmm 4 va_x_xmm4
(va_upd_xmm 3 va_x_xmm3 (va_upd_xmm 2 va_x_xmm2 (va_upd_xmm 1 va_x_xmm1 (va_upd_xmm 0 va_x_xmm0
(va_upd_reg64 rR15 va_x_r15 (va_upd_reg64 rR14 va_x_r14 (va_upd_reg64 rR13 va_x_r13
(va_upd_reg64 rR12 va_x_r12 (va_upd_reg64 rR11 va_x_r11 (va_upd_reg64 rR10 va_x_r10
(va_upd_reg64 rR9 va_x_r9 (va_upd_reg64 rR8 va_x_r8 (va_upd_reg64 rRbp va_x_rbp (va_upd_reg64
rRsp va_x_rsp (va_upd_reg64 rRsi va_x_rsi (va_upd_reg64 rRdi va_x_rdi (va_upd_reg64 rRdx
va_x_rdx (va_upd_reg64 rRcx va_x_rcx (va_upd_reg64 rRbx va_x_rbx (va_upd_reg64 rRax va_x_rax
(va_upd_mem va_x_mem va_s0))))))))))))))))))))))))))))))))))))))))) in va_get_ok va_sM /\ (let
(auth_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ -> va_get_reg64 rRcx va_s0)
(fun _ -> va_get_reg64 rRdi va_s0) in let (auth_num_bytes:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> va_get_reg64 rRdx va_s0) (fun _ -> va_get_reg64
rRsi va_s0) in let (auth_len:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
va_get_reg64 rR8 va_s0) (fun _ -> va_get_reg64 rRdx va_s0) in let (keys_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> va_get_reg64 rR9 va_s0) (fun _ -> va_get_reg64
rRcx va_s0) in let (iv_ptr:(va_int_range 0 18446744073709551615)) = va_if win (fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 0) (va_get_stack va_s0)) (fun
_ -> va_get_reg64 rR8 va_s0) in let (xip:(va_int_range 0 18446744073709551615)) = va_if win
(fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 8) (va_get_stack
va_s0)) (fun _ -> va_get_reg64 rR9 va_s0) in let (abytes_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 16) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 0) (va_get_stack va_s0)) in let (in128x6_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 24) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in let (out128x6_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 32) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in let (len128x6:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 40) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in let (in128_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 48) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 32) (va_get_stack va_s0)) in let (out128_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 56) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in let (len128:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 64) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in let (inout_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 72) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 56) (va_get_stack va_s0)) in let (cipher_num_bytes:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 80) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in let (scratch_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 88) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in let (tag_ptr:(va_int_range 0
18446744073709551615)) = va_if win (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp
va_s0 + 40 + 96) (va_get_stack va_s0)) (fun _ -> Vale.X64.Stack_i.load_stack64 (va_get_reg64
rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in Vale.X64.Decls.modifies_mem
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 iv_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 scratch_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128x6_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128_b)
(Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 inout_b))))) (va_get_mem va_s0)
(va_get_mem va_sM) /\ cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ (let iv_BE =
Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in let auth_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0) auth_b)
(Vale.X64.Decls.s128 (va_get_mem va_s0) abytes_b) in let auth_bytes = FStar.Seq.Base.slice
#Vale.Def.Types_s.nat8 (Vale.Def.Types_s.le_seq_quad32_to_bytes auth_raw_quads) 0
auth_num_bytes in let cipher_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32
(FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0)
in128x6_b) (Vale.X64.Decls.s128 (va_get_mem va_s0) in128_b)) (Vale.X64.Decls.s128 (va_get_mem
va_s0) inout_b) in let cipher_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes cipher_raw_quads) 0 cipher_num_bytes in let
plain_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32 (FStar.Seq.Base.append
#Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_sM) out128x6_b) (Vale.X64.Decls.s128
(va_get_mem va_sM) out128_b)) (Vale.X64.Decls.s128 (va_get_mem va_sM) inout_b) in let
plain_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes plain_raw_quads) 0 cipher_num_bytes in let
expected_tag = Vale.Def.Types_s.le_quad32_to_bytes (Vale.X64.Decls.buffer128_read tag_b 0
(va_get_mem va_s0)) in l_and (l_and (l_and (l_and (FStar.Seq.Base.length #Vale.Def.Types_s.nat8
auth_bytes < pow2_32) (FStar.Seq.Base.length #Vale.Def.Types_s.nat8 cipher_bytes < pow2_32))
(Vale.AES.AES_common_s.is_aes_key alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)))
(plain_bytes == __proj__Mktuple2__item___1 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag))) (va_get_reg64 rRax va_sM = 0 ==
__proj__Mktuple2__item___2 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag)) /\ va_get_reg64 rRsp va_sM == va_get_reg64 rRsp va_s0 /\
(win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\ (win ==> va_get_reg64 rRbp
va_sM == va_get_reg64 rRbp va_s0) /\ (win ==> va_get_reg64 rRdi va_sM == va_get_reg64 rRdi
va_s0) /\ (win ==> va_get_reg64 rRsi va_sM == va_get_reg64 rRsi va_s0) /\ (win ==> va_get_reg64
rR12 va_sM == va_get_reg64 rR12 va_s0) /\ (win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (win ==> va_get_reg64
rR15 va_sM == va_get_reg64 rR15 va_s0) /\ (win ==> va_get_xmm 6 va_sM == va_get_xmm 6 va_s0) /\
(win ==> va_get_xmm 7 va_sM == va_get_xmm 7 va_s0) /\ (win ==> va_get_xmm 8 va_sM == va_get_xmm
8 va_s0) /\ (win ==> va_get_xmm 9 va_sM == va_get_xmm 9 va_s0) /\ (win ==> va_get_xmm 10 va_sM
== va_get_xmm 10 va_s0) /\ (win ==> va_get_xmm 11 va_sM == va_get_xmm 11 va_s0) /\ (win ==>
va_get_xmm 12 va_sM == va_get_xmm 12 va_s0) /\ (win ==> va_get_xmm 13 va_sM == va_get_xmm 13
va_s0) /\ (win ==> va_get_xmm 14 va_sM == va_get_xmm 14 va_s0) /\ (win ==> va_get_xmm 15 va_sM
== va_get_xmm 15 va_s0) /\ (~win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\
(~win ==> va_get_reg64 rRbp va_sM == va_get_reg64 rRbp va_s0) /\ (~win ==> va_get_reg64 rR12
va_sM == va_get_reg64 rR12 va_s0) /\ (~win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (~win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (~win ==>
va_get_reg64 rR15 va_sM == va_get_reg64 rR15 va_s0))) ==> va_k va_sM (()))) | {
"file_name": "obj/Vale.AES.X64.GCMdecryptOpt.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 79,
"end_line": 813,
"start_col": 0,
"start_line": 556
} | module Vale.AES.X64.GCMdecryptOpt
open Vale.Def.Prop_s
open Vale.Def.Opaque_s
open FStar.Seq
open Vale.Def.Words_s
open Vale.Def.Words.Seq_s
open Vale.Def.Types_s
open Vale.Arch.Types
open Vale.Arch.HeapImpl
open Vale.AES.AES_s
open Vale.AES.GCTR_s
open Vale.AES.GCTR
open Vale.AES.GCM
open Vale.AES.GHash_s
open Vale.AES.GHash
open Vale.AES.GCM_s
open Vale.AES.X64.AES
open Vale.AES.GF128_s
open Vale.AES.GF128
open Vale.Poly1305.Math
open Vale.AES.GCM_helpers
open Vale.AES.X64.GHash
open Vale.AES.X64.GCTR
open Vale.X64.Machine_s
open Vale.X64.Memory
open Vale.X64.Stack_i
open Vale.X64.State
open Vale.X64.Decls
open Vale.X64.InsBasic
open Vale.X64.InsMem
open Vale.X64.InsVector
open Vale.X64.InsStack
open Vale.X64.InsAes
open Vale.X64.QuickCode
open Vale.X64.QuickCodes
open Vale.AES.X64.GF128_Mul
open Vale.X64.Stack
open Vale.X64.CPU_Features_s
open Vale.Math.Poly2.Bits_s
open Vale.AES.X64.AESopt
open Vale.AES.X64.AESGCM
open Vale.AES.X64.AESopt2
open Vale.Lib.Meta
open Vale.AES.X64.GCMencryptOpt
open Vale.AES.OptPublic
let aes_reqs
(alg:algorithm) (key:seq nat32) (round_keys:seq quad32) (keys_b:buffer128)
(key_ptr:int) (heap0:vale_heap) (layout:vale_heap_layout) : prop0
=
aesni_enabled /\ avx_enabled /\
(alg = AES_128 \/ alg = AES_256) /\
is_aes_key_LE alg key /\
length(round_keys) == nr(alg) + 1 /\
round_keys == key_to_round_keys_LE alg key /\
validSrcAddrs128 heap0 key_ptr keys_b (nr alg + 1) layout Secret /\
s128 heap0 keys_b == round_keys
#reset-options "--z3rlimit 100 --max_ifuel 0"
//-- Gcm_blocks_decrypt_stdcall
val va_code_Gcm_blocks_decrypt_stdcall : win:bool -> alg:algorithm -> Tot va_code
val va_codegen_success_Gcm_blocks_decrypt_stdcall : win:bool -> alg:algorithm -> Tot va_pbool
let va_req_Gcm_blocks_decrypt_stdcall (va_b0:va_code) (va_s0:va_state) (win:bool) (alg:algorithm)
(auth_b:buffer128) (auth_bytes:nat64) (auth_num:nat64) (keys_b:buffer128) (iv_b:buffer128)
(iv:supported_iv_LE) (hkeys_b:buffer128) (abytes_b:buffer128) (in128x6_b:buffer128)
(out128x6_b:buffer128) (len128x6_num:nat64) (in128_b:buffer128) (out128_b:buffer128)
(len128_num:nat64) (inout_b:buffer128) (cipher_num:nat64) (scratch_b:buffer128) (tag_b:buffer128)
(key:(seq nat32)) : prop =
(va_require_total va_b0 (va_code_Gcm_blocks_decrypt_stdcall win alg) va_s0 /\ va_get_ok va_s0 /\
(let (auth_ptr:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rRcx va_s0
else va_get_reg64 rRdi va_s0) in let (auth_num_bytes:(va_int_range 0 18446744073709551615)) =
(if win then va_get_reg64 rRdx va_s0 else va_get_reg64 rRsi va_s0) in let
(auth_len:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rR8 va_s0 else
va_get_reg64 rRdx va_s0) in let (keys_ptr:(va_int_range 0 18446744073709551615)) = (if win then
va_get_reg64 rR9 va_s0 else va_get_reg64 rRcx va_s0) in let (iv_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 0) (va_get_stack va_s0) else va_get_reg64 rR8 va_s0) in let (xip:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 8) (va_get_stack va_s0) else va_get_reg64 rR9 va_s0) in let (abytes_ptr:(va_int_range
0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0
+ 40 + 16) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8
+ 0) (va_get_stack va_s0)) in let (in128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in
let (out128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in let
(len128x6:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in let (in128_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 48) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
32) (va_get_stack va_s0)) in let (out128_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in
let (len128:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in let (inout_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 72) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
56) (va_get_stack va_s0)) in let (cipher_num_bytes:(va_int_range 0 18446744073709551615)) = (if
win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in
let (scratch_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in let
(tag_ptr:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in sse_enabled /\ movbe_enabled /\
va_get_reg64 rRsp va_s0 == Vale.X64.Stack_i.init_rsp (va_get_stack va_s0) /\
Vale.X64.Memory.is_initial_heap (va_get_mem_layout va_s0) (va_get_mem va_s0) /\ (~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 0) (va_get_stack va_s0)
Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 32) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 56) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 0) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 8) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 16) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 48) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 72) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
auth_len == auth_num /\ auth_num_bytes == auth_bytes /\ len128x6 == len128x6_num /\ len128 ==
len128_num /\ cipher_num_bytes == cipher_num /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem
va_s0) auth_ptr auth_b auth_len (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) abytes_ptr abytes_b 1 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) iv_ptr iv_b 1
(va_get_mem_layout va_s0) Public /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128x6_ptr in128x6_b len128x6 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) out128x6_ptr out128x6_b len128x6
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128_ptr in128_b len128 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128
(va_get_mem va_s0) out128_ptr out128_b len128 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) inout_ptr inout_b 1 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) scratch_ptr scratch_b 9
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) xip
hkeys_b 8 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem
va_s0) tag_ptr tag_b 1 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.buffer_disjoints128
tag_b ([auth_b; abytes_b; iv_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; scratch_b])
/\ Vale.X64.Decls.buffer_disjoints128 iv_b ([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b;
in128_b; out128_b; inout_b; scratch_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128
scratch_b ([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b;
hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 inout_b ([keys_b; auth_b; abytes_b; in128x6_b;
out128x6_b; in128_b; out128_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 auth_b ([keys_b;
abytes_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 abytes_b ([keys_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 out128x6_b ([keys_b; auth_b; abytes_b; hkeys_b; in128_b;
inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128x6_b ([keys_b; auth_b; abytes_b; hkeys_b;
in128_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 out128_b ([keys_b; auth_b; abytes_b;
hkeys_b; in128x6_b; out128x6_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128_b
([keys_b; auth_b; abytes_b; hkeys_b; in128x6_b; out128x6_b; inout_b]) /\
(Vale.X64.Decls.buffers_disjoint128 in128x6_b out128x6_b \/ in128x6_b == out128x6_b) /\
(Vale.X64.Decls.buffers_disjoint128 in128_b out128_b \/ in128_b == out128_b) /\ auth_ptr + 16
`op_Multiply` auth_len < pow2_64 /\ in128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\
out128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\ in128_ptr + 16 `op_Multiply` len128 <
pow2_64 /\ out128_ptr + 16 `op_Multiply` len128 < pow2_64 /\ inout_ptr + 16 < pow2_64 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 auth_b == auth_len /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 abytes_b == 1 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128x6_b ==
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 out128x6_b /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b == Vale.X64.Decls.buffer_length
#Vale.X64.Memory.vuint128 out128_b /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128
in128x6_b == len128x6 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b ==
len128 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 inout_b == 1 /\
cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ xip + 32 < pow2_64 /\
Vale.X64.Memory.buffer_addr #Vale.X64.Memory.vuint128 keys_b (va_get_mem va_s0) + 128 < pow2_64
/\ len128x6 `op_Modulus` 6 == 0 /\ (len128x6 > 0 ==> len128x6 >= 6) /\ 12 + len128x6 + 6 <
pow2_32 /\ (va_mul_nat len128x6 (128 `op_Division` 8) + va_mul_nat len128 (128 `op_Division` 8)
<= cipher_num_bytes /\ cipher_num_bytes < va_mul_nat len128x6 (128 `op_Division` 8) +
va_mul_nat len128 (128 `op_Division` 8) + 128 `op_Division` 8) /\ (va_mul_nat auth_len (128
`op_Division` 8) <= auth_num_bytes /\ auth_num_bytes < va_mul_nat auth_len (128 `op_Division`
8) + 128 `op_Division` 8) /\ aes_reqs alg key (Vale.X64.Decls.buffer128_as_seq (va_get_mem
va_s0) keys_b) keys_b keys_ptr (va_get_mem va_s0) (va_get_mem_layout va_s0) /\
pclmulqdq_enabled /\ Vale.AES.OptPublic.hkeys_reqs_pub (Vale.X64.Decls.s128 (va_get_mem va_s0)
hkeys_b) (Vale.Def.Types_s.reverse_bytes_quad32 (Vale.AES.AES_s.aes_encrypt_LE alg key
(Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0))) /\ (let h_LE =
Vale.AES.AES_s.aes_encrypt_LE alg key (Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0)
in let iv_BE = Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in iv_BE ==
Vale.AES.GCM_s.compute_iv_BE h_LE iv)))
let va_ens_Gcm_blocks_decrypt_stdcall (va_b0:va_code) (va_s0:va_state) (win:bool) (alg:algorithm)
(auth_b:buffer128) (auth_bytes:nat64) (auth_num:nat64) (keys_b:buffer128) (iv_b:buffer128)
(iv:supported_iv_LE) (hkeys_b:buffer128) (abytes_b:buffer128) (in128x6_b:buffer128)
(out128x6_b:buffer128) (len128x6_num:nat64) (in128_b:buffer128) (out128_b:buffer128)
(len128_num:nat64) (inout_b:buffer128) (cipher_num:nat64) (scratch_b:buffer128) (tag_b:buffer128)
(key:(seq nat32)) (va_sM:va_state) (va_fM:va_fuel) : prop =
(va_req_Gcm_blocks_decrypt_stdcall va_b0 va_s0 win alg auth_b auth_bytes auth_num keys_b iv_b iv
hkeys_b abytes_b in128x6_b out128x6_b len128x6_num in128_b out128_b len128_num inout_b
cipher_num scratch_b tag_b key /\ va_ensure_total va_b0 va_s0 va_sM va_fM /\ va_get_ok va_sM /\
(let (auth_ptr:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rRcx va_s0
else va_get_reg64 rRdi va_s0) in let (auth_num_bytes:(va_int_range 0 18446744073709551615)) =
(if win then va_get_reg64 rRdx va_s0 else va_get_reg64 rRsi va_s0) in let
(auth_len:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rR8 va_s0 else
va_get_reg64 rRdx va_s0) in let (keys_ptr:(va_int_range 0 18446744073709551615)) = (if win then
va_get_reg64 rR9 va_s0 else va_get_reg64 rRcx va_s0) in let (iv_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 0) (va_get_stack va_s0) else va_get_reg64 rR8 va_s0) in let (xip:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 8) (va_get_stack va_s0) else va_get_reg64 rR9 va_s0) in let (abytes_ptr:(va_int_range
0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0
+ 40 + 16) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8
+ 0) (va_get_stack va_s0)) in let (in128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in
let (out128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in let
(len128x6:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in let (in128_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 48) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
32) (va_get_stack va_s0)) in let (out128_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in
let (len128:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in let (inout_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 72) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
56) (va_get_stack va_s0)) in let (cipher_num_bytes:(va_int_range 0 18446744073709551615)) = (if
win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in
let (scratch_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in let
(tag_ptr:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in Vale.X64.Decls.modifies_mem
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 iv_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 scratch_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128x6_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128_b)
(Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 inout_b))))) (va_get_mem va_s0)
(va_get_mem va_sM) /\ cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ (let iv_BE =
Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in let auth_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0) auth_b)
(Vale.X64.Decls.s128 (va_get_mem va_s0) abytes_b) in let auth_bytes = FStar.Seq.Base.slice
#Vale.Def.Types_s.nat8 (Vale.Def.Types_s.le_seq_quad32_to_bytes auth_raw_quads) 0
auth_num_bytes in let cipher_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32
(FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0)
in128x6_b) (Vale.X64.Decls.s128 (va_get_mem va_s0) in128_b)) (Vale.X64.Decls.s128 (va_get_mem
va_s0) inout_b) in let cipher_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes cipher_raw_quads) 0 cipher_num_bytes in let
plain_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32 (FStar.Seq.Base.append
#Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_sM) out128x6_b) (Vale.X64.Decls.s128
(va_get_mem va_sM) out128_b)) (Vale.X64.Decls.s128 (va_get_mem va_sM) inout_b) in let
plain_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes plain_raw_quads) 0 cipher_num_bytes in let
expected_tag = Vale.Def.Types_s.le_quad32_to_bytes (Vale.X64.Decls.buffer128_read tag_b 0
(va_get_mem va_s0)) in l_and (l_and (l_and (l_and (FStar.Seq.Base.length #Vale.Def.Types_s.nat8
auth_bytes < pow2_32) (FStar.Seq.Base.length #Vale.Def.Types_s.nat8 cipher_bytes < pow2_32))
(Vale.AES.AES_common_s.is_aes_key alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)))
(plain_bytes == __proj__Mktuple2__item___1 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag))) (va_get_reg64 rRax va_sM = 0 ==
__proj__Mktuple2__item___2 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag)) /\ va_get_reg64 rRsp va_sM == va_get_reg64 rRsp va_s0 /\
(win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\ (win ==> va_get_reg64 rRbp
va_sM == va_get_reg64 rRbp va_s0) /\ (win ==> va_get_reg64 rRdi va_sM == va_get_reg64 rRdi
va_s0) /\ (win ==> va_get_reg64 rRsi va_sM == va_get_reg64 rRsi va_s0) /\ (win ==> va_get_reg64
rR12 va_sM == va_get_reg64 rR12 va_s0) /\ (win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (win ==> va_get_reg64
rR15 va_sM == va_get_reg64 rR15 va_s0) /\ (win ==> va_get_xmm 6 va_sM == va_get_xmm 6 va_s0) /\
(win ==> va_get_xmm 7 va_sM == va_get_xmm 7 va_s0) /\ (win ==> va_get_xmm 8 va_sM == va_get_xmm
8 va_s0) /\ (win ==> va_get_xmm 9 va_sM == va_get_xmm 9 va_s0) /\ (win ==> va_get_xmm 10 va_sM
== va_get_xmm 10 va_s0) /\ (win ==> va_get_xmm 11 va_sM == va_get_xmm 11 va_s0) /\ (win ==>
va_get_xmm 12 va_sM == va_get_xmm 12 va_s0) /\ (win ==> va_get_xmm 13 va_sM == va_get_xmm 13
va_s0) /\ (win ==> va_get_xmm 14 va_sM == va_get_xmm 14 va_s0) /\ (win ==> va_get_xmm 15 va_sM
== va_get_xmm 15 va_s0) /\ (~win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\
(~win ==> va_get_reg64 rRbp va_sM == va_get_reg64 rRbp va_s0) /\ (~win ==> va_get_reg64 rR12
va_sM == va_get_reg64 rR12 va_s0) /\ (~win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (~win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (~win ==>
va_get_reg64 rR15 va_sM == va_get_reg64 rR15 va_s0))) /\ va_state_eq va_sM
(va_update_stackTaint va_sM (va_update_stack va_sM (va_update_flags va_sM (va_update_mem_layout
va_sM (va_update_mem_heaplet 6 va_sM (va_update_mem_heaplet 5 va_sM (va_update_mem_heaplet 3
va_sM (va_update_mem_heaplet 2 va_sM (va_update_mem_heaplet 1 va_sM (va_update_xmm 15 va_sM
(va_update_xmm 14 va_sM (va_update_xmm 13 va_sM (va_update_xmm 12 va_sM (va_update_xmm 11 va_sM
(va_update_xmm 10 va_sM (va_update_xmm 9 va_sM (va_update_xmm 8 va_sM (va_update_xmm 7 va_sM
(va_update_xmm 6 va_sM (va_update_xmm 5 va_sM (va_update_xmm 4 va_sM (va_update_xmm 3 va_sM
(va_update_xmm 2 va_sM (va_update_xmm 1 va_sM (va_update_xmm 0 va_sM (va_update_reg64 rR15
va_sM (va_update_reg64 rR14 va_sM (va_update_reg64 rR13 va_sM (va_update_reg64 rR12 va_sM
(va_update_reg64 rR11 va_sM (va_update_reg64 rR10 va_sM (va_update_reg64 rR9 va_sM
(va_update_reg64 rR8 va_sM (va_update_reg64 rRbp va_sM (va_update_reg64 rRsp va_sM
(va_update_reg64 rRsi va_sM (va_update_reg64 rRdi va_sM (va_update_reg64 rRdx va_sM
(va_update_reg64 rRcx va_sM (va_update_reg64 rRbx va_sM (va_update_reg64 rRax va_sM
(va_update_ok va_sM (va_update_mem va_sM va_s0))))))))))))))))))))))))))))))))))))))))))))
val va_lemma_Gcm_blocks_decrypt_stdcall : va_b0:va_code -> va_s0:va_state -> win:bool ->
alg:algorithm -> auth_b:buffer128 -> auth_bytes:nat64 -> auth_num:nat64 -> keys_b:buffer128 ->
iv_b:buffer128 -> iv:supported_iv_LE -> hkeys_b:buffer128 -> abytes_b:buffer128 ->
in128x6_b:buffer128 -> out128x6_b:buffer128 -> len128x6_num:nat64 -> in128_b:buffer128 ->
out128_b:buffer128 -> len128_num:nat64 -> inout_b:buffer128 -> cipher_num:nat64 ->
scratch_b:buffer128 -> tag_b:buffer128 -> key:(seq nat32)
-> Ghost (va_state & va_fuel)
(requires (va_require_total va_b0 (va_code_Gcm_blocks_decrypt_stdcall win alg) va_s0 /\ va_get_ok
va_s0 /\ (let (auth_ptr:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rRcx
va_s0 else va_get_reg64 rRdi va_s0) in let (auth_num_bytes:(va_int_range 0
18446744073709551615)) = (if win then va_get_reg64 rRdx va_s0 else va_get_reg64 rRsi va_s0) in
let (auth_len:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rR8 va_s0 else
va_get_reg64 rRdx va_s0) in let (keys_ptr:(va_int_range 0 18446744073709551615)) = (if win then
va_get_reg64 rR9 va_s0 else va_get_reg64 rRcx va_s0) in let (iv_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 0) (va_get_stack va_s0) else va_get_reg64 rR8 va_s0) in let (xip:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 8) (va_get_stack va_s0) else va_get_reg64 rR9 va_s0) in let (abytes_ptr:(va_int_range
0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0
+ 40 + 16) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8
+ 0) (va_get_stack va_s0)) in let (in128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in
let (out128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in let
(len128x6:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in let (in128_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 48) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
32) (va_get_stack va_s0)) in let (out128_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in
let (len128:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in let (inout_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 72) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
56) (va_get_stack va_s0)) in let (cipher_num_bytes:(va_int_range 0 18446744073709551615)) = (if
win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in
let (scratch_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in let
(tag_ptr:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in sse_enabled /\ movbe_enabled /\
va_get_reg64 rRsp va_s0 == Vale.X64.Stack_i.init_rsp (va_get_stack va_s0) /\
Vale.X64.Memory.is_initial_heap (va_get_mem_layout va_s0) (va_get_mem va_s0) /\ (~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 0) (va_get_stack va_s0)
Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 32) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 56) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (~win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(~win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 0) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 8) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 16) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 48) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 72) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
(win ==> Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack
va_s0) Public (va_get_stackTaint va_s0)) /\ (win ==> Vale.X64.Stack_i.valid_stack_slot64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) Public (va_get_stackTaint va_s0)) /\
auth_len == auth_num /\ auth_num_bytes == auth_bytes /\ len128x6 == len128x6_num /\ len128 ==
len128_num /\ cipher_num_bytes == cipher_num /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem
va_s0) auth_ptr auth_b auth_len (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) abytes_ptr abytes_b 1 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) iv_ptr iv_b 1
(va_get_mem_layout va_s0) Public /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128x6_ptr in128x6_b len128x6 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) out128x6_ptr out128x6_b len128x6
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128_ptr in128_b len128 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128
(va_get_mem va_s0) out128_ptr out128_b len128 (va_get_mem_layout va_s0) Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) inout_ptr inout_b 1 (va_get_mem_layout
va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0) scratch_ptr scratch_b 9
(va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0) xip
hkeys_b 8 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.validDstAddrs128 (va_get_mem
va_s0) tag_ptr tag_b 1 (va_get_mem_layout va_s0) Secret /\ Vale.X64.Decls.buffer_disjoints128
tag_b ([auth_b; abytes_b; iv_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; scratch_b])
/\ Vale.X64.Decls.buffer_disjoints128 iv_b ([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b;
in128_b; out128_b; inout_b; scratch_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128
scratch_b ([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b;
hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 inout_b ([keys_b; auth_b; abytes_b; in128x6_b;
out128x6_b; in128_b; out128_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 auth_b ([keys_b;
abytes_b; hkeys_b]) /\ Vale.X64.Decls.buffer_disjoints128 abytes_b ([keys_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 out128x6_b ([keys_b; auth_b; abytes_b; hkeys_b; in128_b;
inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128x6_b ([keys_b; auth_b; abytes_b; hkeys_b;
in128_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 out128_b ([keys_b; auth_b; abytes_b;
hkeys_b; in128x6_b; out128x6_b; inout_b]) /\ Vale.X64.Decls.buffer_disjoints128 in128_b
([keys_b; auth_b; abytes_b; hkeys_b; in128x6_b; out128x6_b; inout_b]) /\
(Vale.X64.Decls.buffers_disjoint128 in128x6_b out128x6_b \/ in128x6_b == out128x6_b) /\
(Vale.X64.Decls.buffers_disjoint128 in128_b out128_b \/ in128_b == out128_b) /\ auth_ptr + 16
`op_Multiply` auth_len < pow2_64 /\ in128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\
out128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\ in128_ptr + 16 `op_Multiply` len128 <
pow2_64 /\ out128_ptr + 16 `op_Multiply` len128 < pow2_64 /\ inout_ptr + 16 < pow2_64 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 auth_b == auth_len /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 abytes_b == 1 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128x6_b ==
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 out128x6_b /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b == Vale.X64.Decls.buffer_length
#Vale.X64.Memory.vuint128 out128_b /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128
in128x6_b == len128x6 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b ==
len128 /\ Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 inout_b == 1 /\
cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ xip + 32 < pow2_64 /\
Vale.X64.Memory.buffer_addr #Vale.X64.Memory.vuint128 keys_b (va_get_mem va_s0) + 128 < pow2_64
/\ len128x6 `op_Modulus` 6 == 0 /\ (len128x6 > 0 ==> len128x6 >= 6) /\ 12 + len128x6 + 6 <
pow2_32 /\ (va_mul_nat len128x6 (128 `op_Division` 8) + va_mul_nat len128 (128 `op_Division` 8)
<= cipher_num_bytes /\ cipher_num_bytes < va_mul_nat len128x6 (128 `op_Division` 8) +
va_mul_nat len128 (128 `op_Division` 8) + 128 `op_Division` 8) /\ (va_mul_nat auth_len (128
`op_Division` 8) <= auth_num_bytes /\ auth_num_bytes < va_mul_nat auth_len (128 `op_Division`
8) + 128 `op_Division` 8) /\ aes_reqs alg key (Vale.X64.Decls.buffer128_as_seq (va_get_mem
va_s0) keys_b) keys_b keys_ptr (va_get_mem va_s0) (va_get_mem_layout va_s0) /\
pclmulqdq_enabled /\ Vale.AES.OptPublic.hkeys_reqs_pub (Vale.X64.Decls.s128 (va_get_mem va_s0)
hkeys_b) (Vale.Def.Types_s.reverse_bytes_quad32 (Vale.AES.AES_s.aes_encrypt_LE alg key
(Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0))) /\ (let h_LE =
Vale.AES.AES_s.aes_encrypt_LE alg key (Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0)
in let iv_BE = Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in iv_BE ==
Vale.AES.GCM_s.compute_iv_BE h_LE iv))))
(ensures (fun (va_sM, va_fM) -> va_ensure_total va_b0 va_s0 va_sM va_fM /\ va_get_ok va_sM /\
(let (auth_ptr:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rRcx va_s0
else va_get_reg64 rRdi va_s0) in let (auth_num_bytes:(va_int_range 0 18446744073709551615)) =
(if win then va_get_reg64 rRdx va_s0 else va_get_reg64 rRsi va_s0) in let
(auth_len:(va_int_range 0 18446744073709551615)) = (if win then va_get_reg64 rR8 va_s0 else
va_get_reg64 rRdx va_s0) in let (keys_ptr:(va_int_range 0 18446744073709551615)) = (if win then
va_get_reg64 rR9 va_s0 else va_get_reg64 rRcx va_s0) in let (iv_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 0) (va_get_stack va_s0) else va_get_reg64 rR8 va_s0) in let (xip:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
32 + 8 + 8) (va_get_stack va_s0) else va_get_reg64 rR9 va_s0) in let (abytes_ptr:(va_int_range
0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0
+ 40 + 16) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8
+ 0) (va_get_stack va_s0)) in let (in128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)) in
let (out128x6_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0)) in let
(len128x6:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0)) in let (in128_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 48) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
32) (va_get_stack va_s0)) in let (out128_ptr:(va_int_range 0 18446744073709551615)) = (if win
then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0)) in
let (len128:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0)) in let (inout_ptr:(va_int_range 0
18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 +
40 + 72) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 +
56) (va_get_stack va_s0)) in let (cipher_num_bytes:(va_int_range 0 18446744073709551615)) = (if
win then Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0)
else Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0)) in
let (scratch_ptr:(va_int_range 0 18446744073709551615)) = (if win then
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0) else
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0)) in let
(tag_ptr:(va_int_range 0 18446744073709551615)) = (if win then Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0) else Vale.X64.Stack_i.load_stack64
(va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0)) in Vale.X64.Decls.modifies_mem
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 iv_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 scratch_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128x6_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 out128_b)
(Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 inout_b))))) (va_get_mem va_s0)
(va_get_mem va_sM) /\ cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ (let iv_BE =
Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in let auth_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0) auth_b)
(Vale.X64.Decls.s128 (va_get_mem va_s0) abytes_b) in let auth_bytes = FStar.Seq.Base.slice
#Vale.Def.Types_s.nat8 (Vale.Def.Types_s.le_seq_quad32_to_bytes auth_raw_quads) 0
auth_num_bytes in let cipher_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32
(FStar.Seq.Base.append #Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_s0)
in128x6_b) (Vale.X64.Decls.s128 (va_get_mem va_s0) in128_b)) (Vale.X64.Decls.s128 (va_get_mem
va_s0) inout_b) in let cipher_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes cipher_raw_quads) 0 cipher_num_bytes in let
plain_raw_quads = FStar.Seq.Base.append #Vale.X64.Decls.quad32 (FStar.Seq.Base.append
#Vale.X64.Decls.quad32 (Vale.X64.Decls.s128 (va_get_mem va_sM) out128x6_b) (Vale.X64.Decls.s128
(va_get_mem va_sM) out128_b)) (Vale.X64.Decls.s128 (va_get_mem va_sM) inout_b) in let
plain_bytes = FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes plain_raw_quads) 0 cipher_num_bytes in let
expected_tag = Vale.Def.Types_s.le_quad32_to_bytes (Vale.X64.Decls.buffer128_read tag_b 0
(va_get_mem va_s0)) in l_and (l_and (l_and (l_and (FStar.Seq.Base.length #Vale.Def.Types_s.nat8
auth_bytes < pow2_32) (FStar.Seq.Base.length #Vale.Def.Types_s.nat8 cipher_bytes < pow2_32))
(Vale.AES.AES_common_s.is_aes_key alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)))
(plain_bytes == __proj__Mktuple2__item___1 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag))) (va_get_reg64 rRax va_sM = 0 ==
__proj__Mktuple2__item___2 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8) #bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg (Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key) iv
cipher_bytes auth_bytes expected_tag)) /\ va_get_reg64 rRsp va_sM == va_get_reg64 rRsp va_s0 /\
(win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\ (win ==> va_get_reg64 rRbp
va_sM == va_get_reg64 rRbp va_s0) /\ (win ==> va_get_reg64 rRdi va_sM == va_get_reg64 rRdi
va_s0) /\ (win ==> va_get_reg64 rRsi va_sM == va_get_reg64 rRsi va_s0) /\ (win ==> va_get_reg64
rR12 va_sM == va_get_reg64 rR12 va_s0) /\ (win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (win ==> va_get_reg64
rR15 va_sM == va_get_reg64 rR15 va_s0) /\ (win ==> va_get_xmm 6 va_sM == va_get_xmm 6 va_s0) /\
(win ==> va_get_xmm 7 va_sM == va_get_xmm 7 va_s0) /\ (win ==> va_get_xmm 8 va_sM == va_get_xmm
8 va_s0) /\ (win ==> va_get_xmm 9 va_sM == va_get_xmm 9 va_s0) /\ (win ==> va_get_xmm 10 va_sM
== va_get_xmm 10 va_s0) /\ (win ==> va_get_xmm 11 va_sM == va_get_xmm 11 va_s0) /\ (win ==>
va_get_xmm 12 va_sM == va_get_xmm 12 va_s0) /\ (win ==> va_get_xmm 13 va_sM == va_get_xmm 13
va_s0) /\ (win ==> va_get_xmm 14 va_sM == va_get_xmm 14 va_s0) /\ (win ==> va_get_xmm 15 va_sM
== va_get_xmm 15 va_s0) /\ (~win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\
(~win ==> va_get_reg64 rRbp va_sM == va_get_reg64 rRbp va_s0) /\ (~win ==> va_get_reg64 rR12
va_sM == va_get_reg64 rR12 va_s0) /\ (~win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13
va_s0) /\ (~win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\ (~win ==>
va_get_reg64 rR15 va_sM == va_get_reg64 rR15 va_s0))) /\ va_state_eq va_sM
(va_update_stackTaint va_sM (va_update_stack va_sM (va_update_flags va_sM (va_update_mem_layout
va_sM (va_update_mem_heaplet 6 va_sM (va_update_mem_heaplet 5 va_sM (va_update_mem_heaplet 3
va_sM (va_update_mem_heaplet 2 va_sM (va_update_mem_heaplet 1 va_sM (va_update_xmm 15 va_sM
(va_update_xmm 14 va_sM (va_update_xmm 13 va_sM (va_update_xmm 12 va_sM (va_update_xmm 11 va_sM
(va_update_xmm 10 va_sM (va_update_xmm 9 va_sM (va_update_xmm 8 va_sM (va_update_xmm 7 va_sM
(va_update_xmm 6 va_sM (va_update_xmm 5 va_sM (va_update_xmm 4 va_sM (va_update_xmm 3 va_sM
(va_update_xmm 2 va_sM (va_update_xmm 1 va_sM (va_update_xmm 0 va_sM (va_update_reg64 rR15
va_sM (va_update_reg64 rR14 va_sM (va_update_reg64 rR13 va_sM (va_update_reg64 rR12 va_sM
(va_update_reg64 rR11 va_sM (va_update_reg64 rR10 va_sM (va_update_reg64 rR9 va_sM
(va_update_reg64 rR8 va_sM (va_update_reg64 rRbp va_sM (va_update_reg64 rRsp va_sM
(va_update_reg64 rRsi va_sM (va_update_reg64 rRdi va_sM (va_update_reg64 rRdx va_sM
(va_update_reg64 rRcx va_sM (va_update_reg64 rRbx va_sM (va_update_reg64 rRax va_sM
(va_update_ok va_sM (va_update_mem va_sM va_s0))))))))))))))))))))))))))))))))))))))))))))) | {
"checked_file": "/",
"dependencies": [
"Vale.X64.State.fsti.checked",
"Vale.X64.Stack_i.fsti.checked",
"Vale.X64.Stack.fsti.checked",
"Vale.X64.QuickCodes.fsti.checked",
"Vale.X64.QuickCode.fst.checked",
"Vale.X64.Memory.fsti.checked",
"Vale.X64.Machine_s.fst.checked",
"Vale.X64.InsVector.fsti.checked",
"Vale.X64.InsStack.fsti.checked",
"Vale.X64.InsMem.fsti.checked",
"Vale.X64.InsBasic.fsti.checked",
"Vale.X64.InsAes.fsti.checked",
"Vale.X64.Flags.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.X64.CPU_Features_s.fst.checked",
"Vale.Poly1305.Math.fsti.checked",
"Vale.Math.Poly2.Bits_s.fsti.checked",
"Vale.Lib.Meta.fsti.checked",
"Vale.Def.Words_s.fsti.checked",
"Vale.Def.Words.Seq_s.fsti.checked",
"Vale.Def.Types_s.fst.checked",
"Vale.Def.Prop_s.fst.checked",
"Vale.Def.Opaque_s.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.Arch.HeapImpl.fsti.checked",
"Vale.AES.X64.GHash.fsti.checked",
"Vale.AES.X64.GF128_Mul.fsti.checked",
"Vale.AES.X64.GCTR.fsti.checked",
"Vale.AES.X64.GCMencryptOpt.fsti.checked",
"Vale.AES.X64.AESopt2.fsti.checked",
"Vale.AES.X64.AESopt.fsti.checked",
"Vale.AES.X64.AESGCM.fsti.checked",
"Vale.AES.X64.AES.fsti.checked",
"Vale.AES.OptPublic.fsti.checked",
"Vale.AES.GHash_s.fst.checked",
"Vale.AES.GHash.fsti.checked",
"Vale.AES.GF128_s.fsti.checked",
"Vale.AES.GF128.fsti.checked",
"Vale.AES.GCTR_s.fst.checked",
"Vale.AES.GCTR.fsti.checked",
"Vale.AES.GCM_s.fst.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"Vale.AES.GCM.fsti.checked",
"Vale.AES.AES_s.fst.checked",
"Vale.AES.AES_common_s.fst.checked",
"prims.fst.checked",
"FStar.Seq.Base.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked"
],
"interface_file": false,
"source_file": "Vale.AES.X64.GCMdecryptOpt.fsti"
} | [
{
"abbrev": false,
"full_module": "Vale.Lib.Basic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Lib.Meta",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESopt2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESGCM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESopt",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Math.Poly2.Bits_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Stack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GF128_Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.QuickCodes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.QuickCode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsAes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsVector",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsMem",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsBasic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Decls",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.State",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Stack_i",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GCTR",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GHash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Poly1305.Math",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GF128",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GF128_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AES",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GHash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCTR",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCTR_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_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": "Vale.Def.Types_s",
"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": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Opaque_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Prop_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Lib.Meta",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESopt2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESGCM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AESopt",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Math.Poly2.Bits_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Stack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GF128_Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.QuickCodes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.QuickCode",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsAes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsVector",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsMem",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.InsBasic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Decls",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.State",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Stack_i",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.Machine_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GCTR",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.GHash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Poly1305.Math",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GF128",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GF128_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64.AES",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GHash",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCTR",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCTR_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_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": "Vale.Def.Types_s",
"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": "FStar.Seq",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Opaque_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Def.Prop_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "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": 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": 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": 100,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
win: Prims.bool ->
alg: Vale.AES.AES_common_s.algorithm ->
auth_b: Vale.X64.Memory.buffer128 ->
auth_bytes: Vale.X64.Memory.nat64 ->
auth_num: Vale.X64.Memory.nat64 ->
keys_b: Vale.X64.Memory.buffer128 ->
iv_b: Vale.X64.Memory.buffer128 ->
iv: Vale.AES.GCM_s.supported_iv_LE ->
hkeys_b: Vale.X64.Memory.buffer128 ->
abytes_b: Vale.X64.Memory.buffer128 ->
in128x6_b: Vale.X64.Memory.buffer128 ->
out128x6_b: Vale.X64.Memory.buffer128 ->
len128x6_num: Vale.X64.Memory.nat64 ->
in128_b: Vale.X64.Memory.buffer128 ->
out128_b: Vale.X64.Memory.buffer128 ->
len128_num: Vale.X64.Memory.nat64 ->
inout_b: Vale.X64.Memory.buffer128 ->
cipher_num: Vale.X64.Memory.nat64 ->
scratch_b: Vale.X64.Memory.buffer128 ->
tag_b: Vale.X64.Memory.buffer128 ->
key: FStar.Seq.Base.seq Vale.X64.Memory.nat32 ->
va_s0: Vale.X64.Decls.va_state ->
va_k: (_: Vale.X64.Decls.va_state -> _: Prims.unit -> Type0)
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.bool",
"Vale.AES.AES_common_s.algorithm",
"Vale.X64.Memory.buffer128",
"Vale.X64.Memory.nat64",
"Vale.AES.GCM_s.supported_iv_LE",
"FStar.Seq.Base.seq",
"Vale.X64.Memory.nat32",
"Vale.X64.Decls.va_state",
"Prims.unit",
"Prims.l_and",
"Prims.b2t",
"Vale.X64.Decls.va_get_ok",
"Vale.X64.CPU_Features_s.sse_enabled",
"Vale.X64.CPU_Features_s.movbe_enabled",
"Prims.eq2",
"Vale.Def.Words_s.nat64",
"Vale.X64.Decls.va_get_reg64",
"Vale.X64.Machine_s.rRsp",
"Vale.X64.Stack_i.init_rsp",
"Vale.X64.Decls.va_get_stack",
"Vale.X64.Memory.is_initial_heap",
"Vale.X64.Decls.va_get_mem_layout",
"Vale.X64.Decls.va_get_mem",
"Prims.l_imp",
"Prims.l_not",
"Vale.X64.Stack_i.valid_stack_slot64",
"Prims.op_Addition",
"Vale.Arch.HeapTypes_s.Public",
"Vale.X64.Decls.va_get_stackTaint",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"Prims.op_LessThan",
"Vale.Def.Words_s.pow2_64",
"Prims.op_LessThanOrEqual",
"Vale.X64.Decls.validSrcAddrs128",
"Vale.Arch.HeapTypes_s.Secret",
"Vale.X64.Decls.validDstAddrs128",
"Vale.X64.Decls.buffer_disjoints128",
"Prims.Cons",
"Prims.Nil",
"Vale.X64.Decls.buffers_disjoint128",
"Prims.op_Multiply",
"Vale.X64.Machine_s.pow2_64",
"Vale.X64.Decls.buffer_length",
"Vale.X64.Memory.vuint128",
"Prims.nat",
"Vale.X64.Machine_s.pow2_32",
"Vale.X64.Memory.buffer_addr",
"Prims.op_Modulus",
"Prims.op_GreaterThan",
"Vale.X64.Decls.va_mul_nat",
"Prims.op_Division",
"Vale.AES.X64.GCMdecryptOpt.aes_reqs",
"Vale.X64.Decls.buffer128_as_seq",
"Vale.X64.CPU_Features_s.pclmulqdq_enabled",
"Vale.AES.OptPublic.hkeys_reqs_pub",
"Vale.X64.Decls.s128",
"Vale.Def.Types_s.reverse_bytes_quad32",
"Vale.AES.AES_s.aes_encrypt_LE",
"Vale.Def.Words_s.Mkfour",
"Vale.Def.Types_s.nat32",
"Vale.Def.Types_s.quad32",
"Vale.AES.GCM_s.compute_iv_BE",
"Vale.X64.Decls.buffer128_read",
"Vale.X64.Decls.va_int_range",
"Vale.X64.Decls.va_if",
"Vale.X64.Stack_i.load_stack64",
"Vale.X64.Machine_s.rR9",
"Vale.X64.Machine_s.rR8",
"Vale.Def.Types_s.nat64",
"Vale.X64.Machine_s.rRcx",
"Vale.X64.Machine_s.rRdx",
"Vale.X64.Machine_s.rRsi",
"Vale.X64.Machine_s.rRdi",
"Prims.l_Forall",
"Vale.X64.InsBasic.vale_heap",
"Vale.X64.Decls.quad32",
"Vale.Arch.HeapImpl.vale_heap_layout",
"Vale.X64.Flags.t",
"Vale.X64.InsBasic.vale_stack",
"Vale.X64.Memory.memtaint",
"Vale.X64.Decls.modifies_mem",
"Vale.X64.Decls.loc_union",
"Vale.X64.Decls.loc_buffer",
"FStar.Seq.Base.length",
"Vale.Def.Types_s.nat8",
"Vale.AES.AES_common_s.is_aes_key",
"Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE",
"FStar.Pervasives.Native.__proj__Mktuple2__item___1",
"Vale.AES.GCM_s.gcm_decrypt_LE",
"Prims.op_Equality",
"Vale.X64.Machine_s.rRax",
"FStar.Pervasives.Native.__proj__Mktuple2__item___2",
"Vale.X64.Machine_s.rRbx",
"Vale.X64.Machine_s.rRbp",
"Vale.X64.Machine_s.rR12",
"Vale.X64.Machine_s.rR13",
"Vale.X64.Machine_s.rR14",
"Vale.X64.Machine_s.rR15",
"Vale.X64.Decls.va_get_xmm",
"Vale.Def.Words_s.nat8",
"Vale.Def.Types_s.le_quad32_to_bytes",
"FStar.Seq.Base.slice",
"Vale.Def.Types_s.le_seq_quad32_to_bytes",
"FStar.Seq.Base.append",
"Vale.X64.State.vale_state",
"Vale.X64.Decls.va_upd_stackTaint",
"Vale.X64.Decls.va_upd_stack",
"Vale.X64.Decls.va_upd_flags",
"Vale.X64.Decls.va_upd_mem_layout",
"Vale.X64.Decls.va_upd_mem_heaplet",
"Vale.X64.Decls.va_upd_xmm",
"Vale.X64.Decls.va_upd_reg64",
"Vale.X64.Machine_s.rR11",
"Vale.X64.Machine_s.rR10",
"Vale.X64.Decls.va_upd_mem"
] | [] | false | false | false | true | true | let va_wp_Gcm_blocks_decrypt_stdcall
(win: bool)
(alg: algorithm)
(auth_b: buffer128)
(auth_bytes auth_num: nat64)
(keys_b iv_b: buffer128)
(iv: supported_iv_LE)
(hkeys_b abytes_b in128x6_b out128x6_b: buffer128)
(len128x6_num: nat64)
(in128_b out128_b: buffer128)
(len128_num: nat64)
(inout_b: buffer128)
(cipher_num: nat64)
(scratch_b tag_b: buffer128)
(key: (seq nat32))
(va_s0: va_state)
(va_k: (va_state -> unit -> Type0))
: Type0 =
| (va_get_ok va_s0 /\
(let auth_ptr:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rRcx va_s0) (fun _ -> va_get_reg64 rRdi va_s0)
in
let auth_num_bytes:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rRdx va_s0) (fun _ -> va_get_reg64 rRsi va_s0)
in
let auth_len:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rR8 va_s0) (fun _ -> va_get_reg64 rRdx va_s0)
in
let keys_ptr:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rR9 va_s0) (fun _ -> va_get_reg64 rRcx va_s0)
in
let iv_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 0)
(va_get_stack va_s0))
(fun _ -> va_get_reg64 rR8 va_s0)
in
let xip:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 8)
(va_get_stack va_s0))
(fun _ -> va_get_reg64 rR9 va_s0)
in
let abytes_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 16) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 0) (va_get_stack va_s0))
in
let in128x6_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0))
in
let out128x6_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16) (va_get_stack va_s0))
in
let len128x6:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 40) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 24) (va_get_stack va_s0))
in
let in128_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 48) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 32) (va_get_stack va_s0))
in
let out128_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40) (va_get_stack va_s0))
in
let len128:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 64) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 48) (va_get_stack va_s0))
in
let inout_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 72) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 56) (va_get_stack va_s0))
in
let cipher_num_bytes:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64) (va_get_stack va_s0))
in
let scratch_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72) (va_get_stack va_s0))
in
let tag_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 96) (va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 80) (va_get_stack va_s0))
in
sse_enabled /\ movbe_enabled /\
va_get_reg64 rRsp va_s0 == Vale.X64.Stack_i.init_rsp (va_get_stack va_s0) /\
Vale.X64.Memory.is_initial_heap (va_get_mem_layout va_s0) (va_get_mem va_s0) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 0)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 8)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 16)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 24)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 32)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 40)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 48)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 56)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 64)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 72)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(~win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 8 + 80)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 0)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 8)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 16)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 24)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 32)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 40)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 48)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 56)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 64)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 72)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 80)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 88)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\
(win ==>
Vale.X64.Stack_i.valid_stack_slot64 (va_get_reg64 rRsp va_s0 + 40 + 96)
(va_get_stack va_s0)
Public
(va_get_stackTaint va_s0)) /\ auth_len == auth_num /\ auth_num_bytes == auth_bytes /\
len128x6 == len128x6_num /\ len128 == len128_num /\ cipher_num_bytes == cipher_num /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
auth_ptr
auth_b
auth_len
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
abytes_ptr
abytes_b
1
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
iv_ptr
iv_b
1
(va_get_mem_layout va_s0)
Public /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128x6_ptr
in128x6_b
len128x6
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
out128x6_ptr
out128x6_b
len128x6
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
in128_ptr
in128_b
len128
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
out128_ptr
out128_b
len128
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
inout_ptr
inout_b
1
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
scratch_ptr
scratch_b
9
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validSrcAddrs128 (va_get_mem va_s0)
xip
hkeys_b
8
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.validDstAddrs128 (va_get_mem va_s0)
tag_ptr
tag_b
1
(va_get_mem_layout va_s0)
Secret /\
Vale.X64.Decls.buffer_disjoints128 tag_b
([auth_b; abytes_b; iv_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; scratch_b]) /\
Vale.X64.Decls.buffer_disjoints128 iv_b
([
keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; scratch_b;
hkeys_b
]) /\
Vale.X64.Decls.buffer_disjoints128 scratch_b
([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; inout_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 inout_b
([keys_b; auth_b; abytes_b; in128x6_b; out128x6_b; in128_b; out128_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 auth_b ([keys_b; abytes_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 abytes_b ([keys_b; hkeys_b]) /\
Vale.X64.Decls.buffer_disjoints128 out128x6_b
([keys_b; auth_b; abytes_b; hkeys_b; in128_b; inout_b]) /\
Vale.X64.Decls.buffer_disjoints128 in128x6_b
([keys_b; auth_b; abytes_b; hkeys_b; in128_b; inout_b]) /\
Vale.X64.Decls.buffer_disjoints128 out128_b
([keys_b; auth_b; abytes_b; hkeys_b; in128x6_b; out128x6_b; inout_b]) /\
Vale.X64.Decls.buffer_disjoints128 in128_b
([keys_b; auth_b; abytes_b; hkeys_b; in128x6_b; out128x6_b; inout_b]) /\
(Vale.X64.Decls.buffers_disjoint128 in128x6_b out128x6_b \/ in128x6_b == out128x6_b) /\
(Vale.X64.Decls.buffers_disjoint128 in128_b out128_b \/ in128_b == out128_b) /\
auth_ptr + 16 `op_Multiply` auth_len < pow2_64 /\
in128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\
out128x6_ptr + 16 `op_Multiply` len128x6 < pow2_64 /\
in128_ptr + 16 `op_Multiply` len128 < pow2_64 /\ out128_ptr + 16 `op_Multiply` len128 < pow2_64 /\
inout_ptr + 16 < pow2_64 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 auth_b == auth_len /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 abytes_b == 1 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128x6_b ==
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 out128x6_b /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b ==
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 out128_b /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128x6_b == len128x6 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 in128_b == len128 /\
Vale.X64.Decls.buffer_length #Vale.X64.Memory.vuint128 inout_b == 1 /\
cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\ xip + 32 < pow2_64 /\
Vale.X64.Memory.buffer_addr #Vale.X64.Memory.vuint128 keys_b (va_get_mem va_s0) + 128 < pow2_64 /\
len128x6 `op_Modulus` 6 == 0 /\ (len128x6 > 0 ==> len128x6 >= 6) /\ 12 + len128x6 + 6 < pow2_32 /\
(va_mul_nat len128x6 (128 `op_Division` 8) + va_mul_nat len128 (128 `op_Division` 8) <=
cipher_num_bytes /\
cipher_num_bytes <
va_mul_nat len128x6 (128 `op_Division` 8) + va_mul_nat len128 (128 `op_Division` 8) +
128
`op_Division`
8) /\
(va_mul_nat auth_len (128 `op_Division` 8) <= auth_num_bytes /\
auth_num_bytes < va_mul_nat auth_len (128 `op_Division` 8) + 128 `op_Division` 8) /\
aes_reqs alg
key
(Vale.X64.Decls.buffer128_as_seq (va_get_mem va_s0) keys_b)
keys_b
keys_ptr
(va_get_mem va_s0)
(va_get_mem_layout va_s0) /\ pclmulqdq_enabled /\
Vale.AES.OptPublic.hkeys_reqs_pub (Vale.X64.Decls.s128 (va_get_mem va_s0) hkeys_b)
(Vale.Def.Types_s.reverse_bytes_quad32 (Vale.AES.AES_s.aes_encrypt_LE alg
key
(Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0))) /\
(let h_LE =
Vale.AES.AES_s.aes_encrypt_LE alg
key
(Vale.Def.Words_s.Mkfour #Vale.Def.Types_s.nat32 0 0 0 0)
in
let iv_BE = Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in
iv_BE == Vale.AES.GCM_s.compute_iv_BE h_LE iv)) /\
(forall (va_x_mem: vale_heap) (va_x_rax: nat64) (va_x_rbx: nat64) (va_x_rcx: nat64)
(va_x_rdx: nat64) (va_x_rdi: nat64) (va_x_rsi: nat64) (va_x_rsp: nat64) (va_x_rbp: nat64)
(va_x_r8: nat64) (va_x_r9: nat64) (va_x_r10: nat64) (va_x_r11: nat64) (va_x_r12: nat64)
(va_x_r13: nat64) (va_x_r14: nat64) (va_x_r15: nat64) (va_x_xmm0: quad32) (va_x_xmm1: quad32)
(va_x_xmm2: quad32) (va_x_xmm3: quad32) (va_x_xmm4: quad32) (va_x_xmm5: quad32)
(va_x_xmm6: quad32) (va_x_xmm7: quad32) (va_x_xmm8: quad32) (va_x_xmm9: quad32)
(va_x_xmm10: quad32) (va_x_xmm11: quad32) (va_x_xmm12: quad32) (va_x_xmm13: quad32)
(va_x_xmm14: quad32) (va_x_xmm15: quad32) (va_x_heap1: vale_heap) (va_x_heap2: vale_heap)
(va_x_heap3: vale_heap) (va_x_heap5: vale_heap) (va_x_heap6: vale_heap)
(va_x_memLayout: vale_heap_layout) (va_x_efl: Vale.X64.Flags.t) (va_x_stack: vale_stack)
(va_x_stackTaint: memtaint).
let va_sM =
va_upd_stackTaint va_x_stackTaint
(va_upd_stack va_x_stack
(va_upd_flags va_x_efl
(va_upd_mem_layout va_x_memLayout
(va_upd_mem_heaplet 6
va_x_heap6
(va_upd_mem_heaplet 5
va_x_heap5
(va_upd_mem_heaplet 3
va_x_heap3
(va_upd_mem_heaplet 2
va_x_heap2
(va_upd_mem_heaplet 1
va_x_heap1
(va_upd_xmm 15
va_x_xmm15
(va_upd_xmm 14
va_x_xmm14
(va_upd_xmm 13
va_x_xmm13
(va_upd_xmm 12
va_x_xmm12
(va_upd_xmm 11
va_x_xmm11
(va_upd_xmm 10
va_x_xmm10
(va_upd_xmm 9
va_x_xmm9
(va_upd_xmm 8
va_x_xmm8
(va_upd_xmm 7
va_x_xmm7
(va_upd_xmm 6
va_x_xmm6
(va_upd_xmm 5
va_x_xmm5
(va_upd_xmm 4
va_x_xmm4
(va_upd_xmm
3
va_x_xmm3
(va_upd_xmm
2
va_x_xmm2
(va_upd_xmm
1
va_x_xmm1
(
va_upd_xmm
0
va_x_xmm0
(
va_upd_reg64
rR15
va_x_r15
(
va_upd_reg64
rR14
va_x_r14
(
va_upd_reg64
rR13
va_x_r13
(
va_upd_reg64
rR12
va_x_r12
(
va_upd_reg64
rR11
va_x_r11
(
va_upd_reg64
rR10
va_x_r10
(
va_upd_reg64
rR9
va_x_r9
(
va_upd_reg64
rR8
va_x_r8
(
va_upd_reg64
rRbp
va_x_rbp
(
va_upd_reg64
rRsp
va_x_rsp
(
va_upd_reg64
rRsi
va_x_rsi
(
va_upd_reg64
rRdi
va_x_rdi
(
va_upd_reg64
rRdx
va_x_rdx
(
va_upd_reg64
rRcx
va_x_rcx
(
va_upd_reg64
rRbx
va_x_rbx
(
va_upd_reg64
rRax
va_x_rax
(
va_upd_mem
va_x_mem
va_s0
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
))
))))))))))
)))))))))))
in
va_get_ok va_sM /\
(let auth_ptr:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rRcx va_s0) (fun _ -> va_get_reg64 rRdi va_s0)
in
let auth_num_bytes:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rRdx va_s0) (fun _ -> va_get_reg64 rRsi va_s0)
in
let auth_len:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rR8 va_s0) (fun _ -> va_get_reg64 rRdx va_s0)
in
let keys_ptr:(va_int_range 0 18446744073709551615) =
va_if win (fun _ -> va_get_reg64 rR9 va_s0) (fun _ -> va_get_reg64 rRcx va_s0)
in
let iv_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 0)
(va_get_stack va_s0))
(fun _ -> va_get_reg64 rR8 va_s0)
in
let xip:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 32 + 8 + 8)
(va_get_stack va_s0))
(fun _ -> va_get_reg64 rR9 va_s0)
in
let abytes_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 16)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 0) (va_get_stack va_s0)
)
in
let in128x6_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 24)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 8) (va_get_stack va_s0)
)
in
let out128x6_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 32)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 16)
(va_get_stack va_s0))
in
let len128x6:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 40)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 24)
(va_get_stack va_s0))
in
let in128_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 48)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 32)
(va_get_stack va_s0))
in
let out128_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 56)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 40)
(va_get_stack va_s0))
in
let len128:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 64)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 48)
(va_get_stack va_s0))
in
let inout_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 72)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 56)
(va_get_stack va_s0))
in
let cipher_num_bytes:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 80)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 64)
(va_get_stack va_s0))
in
let scratch_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 88)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 72)
(va_get_stack va_s0))
in
let tag_ptr:(va_int_range 0 18446744073709551615) =
va_if win
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 40 + 96)
(va_get_stack va_s0))
(fun _ ->
Vale.X64.Stack_i.load_stack64 (va_get_reg64 rRsp va_s0 + 8 + 80)
(va_get_stack va_s0))
in
Vale.X64.Decls.modifies_mem (Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128
iv_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128
scratch_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128
out128x6_b)
(Vale.X64.Decls.loc_union (Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128
out128_b)
(Vale.X64.Decls.loc_buffer #Vale.X64.Memory.vuint128 inout_b)))))
(va_get_mem va_s0)
(va_get_mem va_sM) /\ cipher_num_bytes < pow2_32 /\ auth_num_bytes < pow2_32 /\
(let iv_BE = Vale.X64.Decls.buffer128_read iv_b 0 (va_get_mem va_s0) in
let auth_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32
(Vale.X64.Decls.s128 (va_get_mem va_s0) auth_b)
(Vale.X64.Decls.s128 (va_get_mem va_s0) abytes_b)
in
let auth_bytes =
FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes auth_raw_quads)
0
auth_num_bytes
in
let cipher_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32
(FStar.Seq.Base.append #Vale.X64.Decls.quad32
(Vale.X64.Decls.s128 (va_get_mem va_s0) in128x6_b)
(Vale.X64.Decls.s128 (va_get_mem va_s0) in128_b))
(Vale.X64.Decls.s128 (va_get_mem va_s0) inout_b)
in
let cipher_bytes =
FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes cipher_raw_quads)
0
cipher_num_bytes
in
let plain_raw_quads =
FStar.Seq.Base.append #Vale.X64.Decls.quad32
(FStar.Seq.Base.append #Vale.X64.Decls.quad32
(Vale.X64.Decls.s128 (va_get_mem va_sM) out128x6_b)
(Vale.X64.Decls.s128 (va_get_mem va_sM) out128_b))
(Vale.X64.Decls.s128 (va_get_mem va_sM) inout_b)
in
let plain_bytes =
FStar.Seq.Base.slice #Vale.Def.Types_s.nat8
(Vale.Def.Types_s.le_seq_quad32_to_bytes plain_raw_quads)
0
cipher_num_bytes
in
let expected_tag =
Vale.Def.Types_s.le_quad32_to_bytes (Vale.X64.Decls.buffer128_read tag_b
0
(va_get_mem va_s0))
in
l_and (l_and (l_and (l_and (FStar.Seq.Base.length #Vale.Def.Types_s.nat8 auth_bytes <
pow2_32)
(FStar.Seq.Base.length #Vale.Def.Types_s.nat8 cipher_bytes < pow2_32))
(Vale.AES.AES_common_s.is_aes_key alg
(Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)))
(plain_bytes ==
__proj__Mktuple2__item___1 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8)
#bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg
(Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)
iv
cipher_bytes
auth_bytes
expected_tag)))
(va_get_reg64 rRax va_sM = 0 ==
__proj__Mktuple2__item___2 #(FStar.Seq.Base.seq Vale.Def.Types_s.nat8)
#bool
(Vale.AES.GCM_s.gcm_decrypt_LE alg
(Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE key)
iv
cipher_bytes
auth_bytes
expected_tag)) /\ va_get_reg64 rRsp va_sM == va_get_reg64 rRsp va_s0 /\
(win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\
(win ==> va_get_reg64 rRbp va_sM == va_get_reg64 rRbp va_s0) /\
(win ==> va_get_reg64 rRdi va_sM == va_get_reg64 rRdi va_s0) /\
(win ==> va_get_reg64 rRsi va_sM == va_get_reg64 rRsi va_s0) /\
(win ==> va_get_reg64 rR12 va_sM == va_get_reg64 rR12 va_s0) /\
(win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13 va_s0) /\
(win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\
(win ==> va_get_reg64 rR15 va_sM == va_get_reg64 rR15 va_s0) /\
(win ==> va_get_xmm 6 va_sM == va_get_xmm 6 va_s0) /\
(win ==> va_get_xmm 7 va_sM == va_get_xmm 7 va_s0) /\
(win ==> va_get_xmm 8 va_sM == va_get_xmm 8 va_s0) /\
(win ==> va_get_xmm 9 va_sM == va_get_xmm 9 va_s0) /\
(win ==> va_get_xmm 10 va_sM == va_get_xmm 10 va_s0) /\
(win ==> va_get_xmm 11 va_sM == va_get_xmm 11 va_s0) /\
(win ==> va_get_xmm 12 va_sM == va_get_xmm 12 va_s0) /\
(win ==> va_get_xmm 13 va_sM == va_get_xmm 13 va_s0) /\
(win ==> va_get_xmm 14 va_sM == va_get_xmm 14 va_s0) /\
(win ==> va_get_xmm 15 va_sM == va_get_xmm 15 va_s0) /\
(~win ==> va_get_reg64 rRbx va_sM == va_get_reg64 rRbx va_s0) /\
(~win ==> va_get_reg64 rRbp va_sM == va_get_reg64 rRbp va_s0) /\
(~win ==> va_get_reg64 rR12 va_sM == va_get_reg64 rR12 va_s0) /\
(~win ==> va_get_reg64 rR13 va_sM == va_get_reg64 rR13 va_s0) /\
(~win ==> va_get_reg64 rR14 va_sM == va_get_reg64 rR14 va_s0) /\
(~win ==> va_get_reg64 rR15 va_sM == va_get_reg64 rR15 va_s0))) ==>
va_k va_sM (()))) | false |
Hacl.Impl.Lib.fst | Hacl.Impl.Lib.fill_elems_st | val fill_elems_st : Type | let fill_elems_st =
#t:Type0
-> #a:Type0
-> h0:mem
-> n:size_t
-> output:lbuffer t n
-> refl:(mem -> i:size_nat{i <= v n} -> GTot a)
-> footprint:(i:size_nat{i <= v n} -> GTot
(l:B.loc{B.loc_disjoint l (loc output) /\ B.address_liveness_insensitive_locs `B.loc_includes` l}))
-> spec:(mem -> GTot (i:size_nat{i < v n} -> a -> a & t))
-> impl:(i:size_t{v i < v n} -> Stack unit
(requires fun h ->
modifies (footprint (v i) |+| loc (gsub output 0ul i)) h0 h)
(ensures fun h1 _ h2 ->
(let block1 = gsub output i 1ul in
let c, e = spec h0 (v i) (refl h1 (v i)) in
refl h2 (v i + 1) == c /\
LSeq.index (as_seq h2 block1) 0 == e /\
footprint (v i + 1) `B.loc_includes` footprint (v i) /\
modifies (footprint (v i + 1) |+| (loc block1)) h1 h2))) ->
Stack unit
(requires fun h -> h0 == h /\ live h output)
(ensures fun _ _ h1 -> modifies (footprint (v n) |+| loc output) h0 h1 /\
(let s, o = S.generate_elems (v n) (v n) (spec h0) (refl h0 0) in
refl h1 (v n) == s /\ as_seq #_ #t h1 output == o)) | {
"file_name": "code/bignum/Hacl.Impl.Lib.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 57,
"end_line": 44,
"start_col": 0,
"start_line": 20
} | module Hacl.Impl.Lib
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
module LSeq = Lib.Sequence
module Loops = Lib.LoopCombinators
module S = Hacl.Spec.Lib
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0" | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Lib.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Lib.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Lib",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type | Prims.Tot | [
"total"
] | [] | [
"FStar.Monotonic.HyperStack.mem",
"Lib.IntTypes.size_t",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.size_nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"LowStar.Monotonic.Buffer.loc",
"Prims.l_and",
"LowStar.Monotonic.Buffer.loc_disjoint",
"Lib.Buffer.loc",
"Lib.Buffer.MUT",
"LowStar.Monotonic.Buffer.loc_includes",
"LowStar.Monotonic.Buffer.address_liveness_insensitive_locs",
"Prims.op_LessThan",
"FStar.Pervasives.Native.tuple2",
"Prims.unit",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.gsub",
"FStar.UInt32.__uint_to_t",
"Prims.eq2",
"Prims.op_Addition",
"Lib.Sequence.index",
"Lib.Buffer.as_seq",
"Lib.Buffer.lbuffer_t",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.t",
"Lib.Buffer.live",
"Lib.Sequence.seq",
"Prims.nat",
"Lib.Sequence.length",
"Prims.l_or",
"FStar.Seq.Base.length",
"Prims.logical",
"Hacl.Spec.Lib.generate_elems"
] | [] | false | false | false | true | true | let fill_elems_st =
|
#t: Type0 ->
#a: Type0 ->
h0: mem ->
n: size_t ->
output: lbuffer t n ->
refl: (mem -> i: size_nat{i <= v n} -> GTot a) ->
footprint:
(i: size_nat{i <= v n}
-> GTot
(l:
B.loc
{ B.loc_disjoint l (loc output) /\
B.address_liveness_insensitive_locs `B.loc_includes` l })) ->
spec: (mem -> GTot (i: size_nat{i < v n} -> a -> a & t)) ->
impl:
(i: size_t{v i < v n}
-> Stack unit
(requires fun h -> modifies (footprint (v i) |+| loc (gsub output 0ul i)) h0 h)
(ensures
fun h1 _ h2 ->
(let block1 = gsub output i 1ul in
let c, e = spec h0 (v i) (refl h1 (v i)) in
refl h2 (v i + 1) == c /\ LSeq.index (as_seq h2 block1) 0 == e /\
(footprint (v i + 1)) `B.loc_includes` (footprint (v i)) /\
modifies (footprint (v i + 1) |+| (loc block1)) h1 h2)))
-> Stack unit
(requires fun h -> h0 == h /\ live h output)
(ensures
fun _ _ h1 ->
modifies (footprint (v n) |+| loc output) h0 h1 /\
(let s, o = S.generate_elems (v n) (v n) (spec h0) (refl h0 0) in
refl h1 (v n) == s /\ as_seq #_ #t h1 output == o)) | false |
|
Hacl.Impl.Lib.fst | Hacl.Impl.Lib.lemma_eq_disjoint | val lemma_eq_disjoint:
#a1:Type
-> #a2:Type
-> #a3:Type
-> clen1:size_t
-> clen2:size_t
-> clen3:size_t
-> b1:lbuffer a1 clen1
-> b2:lbuffer a2 clen2
-> b3:lbuffer a3 clen3
-> n:size_t{v n < v clen2 /\ v n < v clen1}
-> h0:mem
-> h1:mem -> Lemma
(requires
live h0 b1 /\ live h0 b2 /\ live h0 b3 /\
eq_or_disjoint b1 b2 /\ disjoint b1 b3 /\ disjoint b2 b3 /\
modifies (loc (gsub b1 0ul n) |+| loc b3) h0 h1)
(ensures
(let b2s = gsub b2 n (clen2 -! n) in
as_seq h0 b2s == as_seq h1 b2s /\
Seq.index (as_seq h0 b2) (v n) == Seq.index (as_seq h1 b2) (v n))) | val lemma_eq_disjoint:
#a1:Type
-> #a2:Type
-> #a3:Type
-> clen1:size_t
-> clen2:size_t
-> clen3:size_t
-> b1:lbuffer a1 clen1
-> b2:lbuffer a2 clen2
-> b3:lbuffer a3 clen3
-> n:size_t{v n < v clen2 /\ v n < v clen1}
-> h0:mem
-> h1:mem -> Lemma
(requires
live h0 b1 /\ live h0 b2 /\ live h0 b3 /\
eq_or_disjoint b1 b2 /\ disjoint b1 b3 /\ disjoint b2 b3 /\
modifies (loc (gsub b1 0ul n) |+| loc b3) h0 h1)
(ensures
(let b2s = gsub b2 n (clen2 -! n) in
as_seq h0 b2s == as_seq h1 b2s /\
Seq.index (as_seq h0 b2) (v n) == Seq.index (as_seq h1 b2) (v n))) | let lemma_eq_disjoint #a1 #a2 #a3 clen1 clen2 clen3 b1 b2 b3 n h0 h1 =
let b1s = gsub b1 0ul n in
let b2s = gsub b2 0ul n in
assert (modifies (loc b1s |+| loc b3) h0 h1);
assert (disjoint b1 b2 ==> Seq.equal (as_seq h0 b2) (as_seq h1 b2));
assert (disjoint b1 b2 ==> Seq.equal (as_seq h0 b2s) (as_seq h1 b2s));
assert (Seq.index (as_seq h1 b2) (v n) == Seq.index (as_seq h1 (gsub b2 n (clen2 -! n))) 0) | {
"file_name": "code/bignum/Hacl.Impl.Lib.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 93,
"end_line": 185,
"start_col": 0,
"start_line": 179
} | module Hacl.Impl.Lib
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module B = LowStar.Buffer
module LSeq = Lib.Sequence
module Loops = Lib.LoopCombinators
module S = Hacl.Spec.Lib
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
inline_for_extraction noextract
let fill_elems_st =
#t:Type0
-> #a:Type0
-> h0:mem
-> n:size_t
-> output:lbuffer t n
-> refl:(mem -> i:size_nat{i <= v n} -> GTot a)
-> footprint:(i:size_nat{i <= v n} -> GTot
(l:B.loc{B.loc_disjoint l (loc output) /\ B.address_liveness_insensitive_locs `B.loc_includes` l}))
-> spec:(mem -> GTot (i:size_nat{i < v n} -> a -> a & t))
-> impl:(i:size_t{v i < v n} -> Stack unit
(requires fun h ->
modifies (footprint (v i) |+| loc (gsub output 0ul i)) h0 h)
(ensures fun h1 _ h2 ->
(let block1 = gsub output i 1ul in
let c, e = spec h0 (v i) (refl h1 (v i)) in
refl h2 (v i + 1) == c /\
LSeq.index (as_seq h2 block1) 0 == e /\
footprint (v i + 1) `B.loc_includes` footprint (v i) /\
modifies (footprint (v i + 1) |+| (loc block1)) h1 h2))) ->
Stack unit
(requires fun h -> h0 == h /\ live h output)
(ensures fun _ _ h1 -> modifies (footprint (v n) |+| loc output) h0 h1 /\
(let s, o = S.generate_elems (v n) (v n) (spec h0) (refl h0 0) in
refl h1 (v n) == s /\ as_seq #_ #t h1 output == o))
inline_for_extraction noextract
val fill_elems : fill_elems_st
let fill_elems #t #a h0 n output refl footprint spec impl =
[@inline_let]
let refl' h (i:nat{i <= v n}) : GTot (S.generate_elem_a t a (v n) i) =
refl h i, as_seq h (gsub output 0ul (size i)) in
[@inline_let]
let footprint' i = footprint i |+| loc (gsub output 0ul (size i)) in
[@inline_let]
let spec' h0 = S.generate_elem_f (v n) (spec h0) in
let h0 = ST.get () in
loop h0 n (S.generate_elem_a t a (v n)) refl' footprint' spec'
(fun i ->
Loops.unfold_repeat_gen (v n) (S.generate_elem_a t a (v n)) (spec' h0) (refl' h0 0) (v i);
impl i;
let h = ST.get() in
FStar.Seq.lemma_split (as_seq h (gsub output 0ul (i +! 1ul))) (v i)
);
let h1 = ST.get () in
assert (refl' h1 (v n) ==
Loops.repeat_gen (v n) (S.generate_elem_a t a (v n)) (S.generate_elem_f (v n) (spec h0)) (refl' h0 0))
inline_for_extraction noextract
val fill_blocks4:
#t:Type0
-> #a:Type0
-> h0:mem
-> n4:size_t{4 * v n4 <= max_size_t}
-> output:lbuffer t (4ul *! n4)
-> refl:(mem -> i:size_nat{i <= 4 * v n4} -> GTot a)
-> footprint:(i:size_nat{i <= 4 * v n4} -> GTot
(l:B.loc{B.loc_disjoint l (loc output) /\ B.address_liveness_insensitive_locs `B.loc_includes` l}))
-> spec:(mem -> GTot (i:size_nat{i < 4 * v n4} -> a -> a & t))
-> impl:(i:size_t{v i < 4 * v n4} -> Stack unit
(requires fun h ->
modifies (footprint (v i) |+| loc (gsub output 0ul i)) h0 h)
(ensures fun h1 _ h2 ->
(let block1 = gsub output i 1ul in
let c, e = spec h0 (v i) (refl h1 (v i)) in
refl h2 (v i + 1) == c /\ LSeq.index (as_seq h2 block1) 0 == e /\
footprint (v i + 1) `B.loc_includes` footprint (v i) /\
modifies (footprint (v i + 1) |+| (loc block1)) h1 h2))) ->
Stack unit
(requires fun h -> h0 == h /\ live h output)
(ensures fun _ _ h1 -> modifies (footprint (4 * v n4) |+| loc output) h0 h1 /\
(let s, o = LSeq.generate_blocks 4 (v n4) (v n4) (Loops.fixed_a a)
(S.generate_blocks4_f #t #a (v n4) (spec h0)) (refl h0 0) in
refl h1 (4 * v n4) == s /\ as_seq #_ #t h1 output == o))
let fill_blocks4 #t #a h0 n4 output refl footprint spec impl =
fill_blocks h0 4ul n4 output (Loops.fixed_a a)
(fun h i -> refl h (4 * i))
(fun i -> footprint (4 * i))
(fun h0 -> S.generate_blocks4_f #t #a (v n4) (spec h0))
(fun i ->
let h1 = ST.get () in
impl (4ul *! i);
impl (4ul *! i +! 1ul);
impl (4ul *! i +! 2ul);
impl (4ul *! i +! 3ul);
let h2 = ST.get () in
assert (
let c0, e0 = spec h0 (4 * v i) (refl h1 (4 * v i)) in
let c1, e1 = spec h0 (4 * v i + 1) c0 in
let c2, e2 = spec h0 (4 * v i + 2) c1 in
let c3, e3 = spec h0 (4 * v i + 3) c2 in
let res = LSeq.create4 e0 e1 e2 e3 in
LSeq.create4_lemma e0 e1 e2 e3;
let res1 = LSeq.sub (as_seq h2 output) (4 * v i) 4 in
refl h2 (4 * v i + 4) == c3 /\
(LSeq.eq_intro res res1; res1 `LSeq.equal` res))
)
inline_for_extraction noextract
val fill_elems4: fill_elems_st
let fill_elems4 #t #a h0 n output refl footprint spec impl =
[@inline_let] let k = n /. 4ul in
let tmp = sub output 0ul (4ul *! k) in
fill_blocks4 #t #a h0 k tmp refl footprint spec (fun i -> impl i);
let h1 = ST.get () in
assert (4 * v k + v (n -! 4ul *! k) = v n);
B.modifies_buffer_elim (B.gsub #t output (4ul *! k) (n -! 4ul *! k)) (footprint (4 * v k) |+| loc tmp) h0 h1;
assert (modifies (footprint (4 * v k) |+| loc (gsub output 0ul (4ul *! k))) h0 h1);
let inv (h:mem) (i:nat{4 * v k <= i /\ i <= v n}) =
modifies (footprint i |+| loc (gsub output 0ul (size i))) h0 h /\
(let (c, res) = Loops.repeat_right (v n / 4 * 4) i (S.generate_elem_a t a (v n))
(S.generate_elem_f (v n) (spec h0)) (refl h1 (4 * v k), as_seq h1 (gsub output 0ul (4ul *! k))) in
refl h i == c /\ as_seq h (gsub output 0ul (size i)) == res) in
Loops.eq_repeat_right (v n / 4 * 4) (v n) (S.generate_elem_a t a (v n))
(S.generate_elem_f (v n) (spec h0)) (refl h1 (4 * v k), as_seq h1 (gsub output 0ul (4ul *! k)));
Lib.Loops.for (k *! 4ul) n inv
(fun i ->
impl i;
let h = ST.get () in
assert (v (i +! 1ul) = v i + 1);
FStar.Seq.lemma_split (as_seq h (gsub output 0ul (i +! 1ul))) (v i);
Loops.unfold_repeat_right (v n / 4 * 4) (v n) (S.generate_elem_a t a (v n))
(S.generate_elem_f (v n) (spec h0)) (refl h1 (4 * v k), as_seq h1 (gsub output 0ul (4ul *! k))) (v i)
);
S.lemma_generate_elems4 (v n) (v n) (spec h0) (refl h0 0)
inline_for_extraction noextract
val lemma_eq_disjoint:
#a1:Type
-> #a2:Type
-> #a3:Type
-> clen1:size_t
-> clen2:size_t
-> clen3:size_t
-> b1:lbuffer a1 clen1
-> b2:lbuffer a2 clen2
-> b3:lbuffer a3 clen3
-> n:size_t{v n < v clen2 /\ v n < v clen1}
-> h0:mem
-> h1:mem -> Lemma
(requires
live h0 b1 /\ live h0 b2 /\ live h0 b3 /\
eq_or_disjoint b1 b2 /\ disjoint b1 b3 /\ disjoint b2 b3 /\
modifies (loc (gsub b1 0ul n) |+| loc b3) h0 h1)
(ensures
(let b2s = gsub b2 n (clen2 -! n) in
as_seq h0 b2s == as_seq h1 b2s /\
Seq.index (as_seq h0 b2) (v n) == Seq.index (as_seq h1 b2) (v n))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.Loops.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Lib.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Impl.Lib.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Lib",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Impl",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
clen1: Lib.IntTypes.size_t ->
clen2: Lib.IntTypes.size_t ->
clen3: Lib.IntTypes.size_t ->
b1: Lib.Buffer.lbuffer a1 clen1 ->
b2: Lib.Buffer.lbuffer a2 clen2 ->
b3: Lib.Buffer.lbuffer a3 clen3 ->
n:
Lib.IntTypes.size_t
{Lib.IntTypes.v n < Lib.IntTypes.v clen2 /\ Lib.IntTypes.v n < Lib.IntTypes.v clen1} ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
Lib.Buffer.live h0 b1 /\ Lib.Buffer.live h0 b2 /\ Lib.Buffer.live h0 b3 /\
Lib.Buffer.eq_or_disjoint b1 b2 /\ Lib.Buffer.disjoint b1 b3 /\ Lib.Buffer.disjoint b2 b3 /\
Lib.Buffer.modifies (Lib.Buffer.loc (Lib.Buffer.gsub b1 0ul n) |+| Lib.Buffer.loc b3) h0 h1)
(ensures
(let b2s = Lib.Buffer.gsub b2 n (clen2 -! n) in
Lib.Buffer.as_seq h0 b2s == Lib.Buffer.as_seq h1 b2s /\
FStar.Seq.Base.index (Lib.Buffer.as_seq h0 b2) (Lib.IntTypes.v n) ==
FStar.Seq.Base.index (Lib.Buffer.as_seq h1 b2) (Lib.IntTypes.v n))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Lib.IntTypes.size_t",
"Lib.Buffer.lbuffer",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"FStar.Monotonic.HyperStack.mem",
"Prims._assert",
"Prims.eq2",
"FStar.Seq.Base.index",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Lib.IntTypes.op_Subtraction_Bang",
"Lib.Buffer.gsub",
"Prims.unit",
"Prims.l_imp",
"Lib.Buffer.disjoint",
"FStar.Seq.Base.equal",
"Lib.Buffer.modifies",
"Lib.Buffer.op_Bar_Plus_Bar",
"Lib.Buffer.loc",
"Lib.Buffer.lbuffer_t",
"FStar.UInt32.__uint_to_t"
] | [] | true | false | true | false | false | let lemma_eq_disjoint #a1 #a2 #a3 clen1 clen2 clen3 b1 b2 b3 n h0 h1 =
| let b1s = gsub b1 0ul n in
let b2s = gsub b2 0ul n in
assert (modifies (loc b1s |+| loc b3) h0 h1);
assert (disjoint b1 b2 ==> Seq.equal (as_seq h0 b2) (as_seq h1 b2));
assert (disjoint b1 b2 ==> Seq.equal (as_seq h0 b2s) (as_seq h1 b2s));
assert (Seq.index (as_seq h1 b2) (v n) == Seq.index (as_seq h1 (gsub b2 n (clen2 -! n))) 0) | 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.