effect
stringclasses 48
values | original_source_type
stringlengths 0
23k
| opens_and_abbrevs
listlengths 2
92
| isa_cross_project_example
bool 1
class | source_definition
stringlengths 9
57.9k
| partial_definition
stringlengths 7
23.3k
| is_div
bool 2
classes | is_type
null | is_proof
bool 2
classes | completed_definiton
stringlengths 1
250k
| dependencies
dict | effect_flags
sequencelengths 0
2
| ideal_premises
sequencelengths 0
236
| mutual_with
sequencelengths 0
11
| file_context
stringlengths 0
407k
| interleaved
bool 1
class | is_simply_typed
bool 2
classes | file_name
stringlengths 5
48
| vconfig
dict | is_simple_lemma
null | source_type
stringlengths 10
23k
| proof_features
sequencelengths 0
1
| name
stringlengths 8
95
| source
dict | verbose_type
stringlengths 1
7.42k
| source_range
dict |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Less_Less_Hat = shift_left | let op_Less_Less_Hat = | false | null | false | shift_left | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.shift_left"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Less_Less_Hat : a: FStar.UInt64.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Less_Less_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 40,
"end_line": 329,
"start_col": 30,
"start_line": 329
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Percent_Hat = rem | let op_Percent_Hat = | false | null | false | rem | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.rem"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Percent_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t{FStar.UInt64.v b <> 0} -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Percent_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t{FStar.UInt64.v b <> 0} -> Prims.Pure FStar.UInt64.t | {
"end_col": 31,
"end_line": 325,
"start_col": 28,
"start_line": 325
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Plus_Question_Hat = add_underspec | let op_Plus_Question_Hat = | false | null | false | add_underspec | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.add_underspec"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *) | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Plus_Question_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Plus_Question_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 47,
"end_line": 316,
"start_col": 34,
"start_line": 316
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Star_Percent_Hat = mul_mod | let op_Star_Percent_Hat = | false | null | false | mul_mod | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.mul_mod"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Star_Percent_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Star_Percent_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 40,
"end_line": 323,
"start_col": 33,
"start_line": 323
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Slash_Hat = div | let op_Slash_Hat = | false | null | false | div | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.div"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Slash_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t{FStar.UInt64.v b <> 0} -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Slash_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t{FStar.UInt64.v b <> 0} -> Prims.Pure FStar.UInt64.t | {
"end_col": 29,
"end_line": 324,
"start_col": 26,
"start_line": 324
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Less_Equals_Hat = lte | let op_Less_Equals_Hat = | false | null | false | lte | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [
"total"
] | [
"FStar.UInt64.lte"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right
unfold let op_Equals_Hat = eq
unfold let op_Greater_Hat = gt
unfold let op_Greater_Equals_Hat = gte | false | true | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Less_Equals_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | [] | FStar.UInt64.op_Less_Equals_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | {
"end_col": 35,
"end_line": 335,
"start_col": 32,
"start_line": 335
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Plus_Hat = add | let op_Plus_Hat = | false | null | false | add | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.add"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Plus_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Plus_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 28,
"end_line": 315,
"start_col": 25,
"start_line": 315
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Star_Hat = mul | let op_Star_Hat = | false | null | false | mul | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.mul"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Star_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Star_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 28,
"end_line": 321,
"start_col": 25,
"start_line": 321
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Greater_Equals_Hat = gte | let op_Greater_Equals_Hat = | false | null | false | gte | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [
"total"
] | [
"FStar.UInt64.gte"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left
unfold let op_Greater_Greater_Hat = shift_right
unfold let op_Equals_Hat = eq | false | true | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Greater_Equals_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | [] | FStar.UInt64.op_Greater_Equals_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | {
"end_col": 38,
"end_line": 333,
"start_col": 35,
"start_line": 333
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Equals_Hat = eq | let op_Equals_Hat = | false | null | false | eq | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [
"total"
] | [
"FStar.UInt64.eq"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor
unfold let op_Less_Less_Hat = shift_left | false | true | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Equals_Hat : a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | [] | FStar.UInt64.op_Equals_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.bool | {
"end_col": 29,
"end_line": 331,
"start_col": 27,
"start_line": 331
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Greater_Greater_Hat = shift_right | let op_Greater_Greater_Hat = | false | null | false | shift_right | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.shift_right"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem
unfold let op_Hat_Hat = logxor
unfold let op_Amp_Hat = logand
unfold let op_Bar_Hat = logor | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Greater_Greater_Hat : a: FStar.UInt64.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Greater_Greater_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 47,
"end_line": 330,
"start_col": 36,
"start_line": 330
} |
|
Prims.Pure | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let op_Amp_Hat = logand | let op_Amp_Hat = | false | null | false | logand | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.logand"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c
#reset-options
(*** Infix notations *)
unfold let op_Plus_Hat = add
unfold let op_Plus_Question_Hat = add_underspec
unfold let op_Plus_Percent_Hat = add_mod
unfold let op_Subtraction_Hat = sub
unfold let op_Subtraction_Question_Hat = sub_underspec
unfold let op_Subtraction_Percent_Hat = sub_mod
unfold let op_Star_Hat = mul
unfold let op_Star_Question_Hat = mul_underspec
unfold let op_Star_Percent_Hat = mul_mod
unfold let op_Slash_Hat = div
unfold let op_Percent_Hat = rem | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val op_Amp_Hat : x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | [] | FStar.UInt64.op_Amp_Hat | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 30,
"end_line": 327,
"start_col": 24,
"start_line": 327
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let minus (a:t) = add_mod (lognot a) (uint_to_t 1) | let minus (a: t) = | false | null | false | add_mod (lognot a) (uint_to_t 1) | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt64.add_mod",
"FStar.UInt64.lognot",
"FStar.UInt64.uint_to_t"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *) | false | true | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val minus : a: FStar.UInt64.t -> FStar.UInt64.t | [] | FStar.UInt64.minus | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> FStar.UInt64.t | {
"end_col": 50,
"end_line": 239,
"start_col": 18,
"start_line": 239
} |
|
Prims.Tot | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let n_minus_one = UInt32.uint_to_t (n - 1) | let n_minus_one = | false | null | false | UInt32.uint_to_t (n - 1) | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [
"total"
] | [
"FStar.UInt32.uint_to_t",
"Prims.op_Subtraction",
"FStar.UInt64.n"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *) | false | true | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val n_minus_one : FStar.UInt32.t | [] | FStar.UInt64.n_minus_one | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | FStar.UInt32.t | {
"end_col": 42,
"end_line": 243,
"start_col": 18,
"start_line": 243
} |
|
Prims.Pure | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\
(v a < v b ==> v c = 0)))
= let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0)))
let gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) = | false | null | false | let x = a in
let y = b in
let x_xor_y = logxor x y in
let x_sub_y = sub_mod x y in
let x_sub_y_xor_y = logxor x_sub_y y in
let q = logor x_xor_y x_sub_y_xor_y in
let x_xor_q = logxor x q in
let x_xor_q_ = shift_right x_xor_q n_minus_one in
let c = sub_mod x_xor_q_ (uint_to_t 1) in
lemma_sub_msbs x y;
lemma_msb_gte (v x) (v y);
lemma_msb_gte (v y) (v x);
c | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.t",
"Prims.unit",
"FStar.UInt.lemma_msb_gte",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt64.lemma_sub_msbs",
"FStar.UInt64.sub_mod",
"FStar.UInt64.uint_to_t",
"FStar.UInt64.shift_right",
"FStar.UInt64.n_minus_one",
"FStar.UInt64.logxor",
"FStar.UInt64.logor",
"Prims.l_True",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_LessThan"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c
private
val lemma_sub_msbs (a:t) (b:t)
: Lemma ((msb (v a) = msb (v b)) ==> (v a < v b <==> msb (v (sub_mod a b))))
(** A constant-time way to compute the [>=] inequality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=0a483a9b431d87eca1b275463c632f8d5551978a
*)
[@ CNoInline ]
let gte_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"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": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0))) | [] | FStar.UInt64.gte_mask | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 5,
"end_line": 311,
"start_col": 3,
"start_line": 299
} |
Prims.Pure | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.UInt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\
(v a <> v b ==> v c = 0)))
= let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b then
begin
logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\
v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n)
end
else
begin
logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n)
end;
c | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0)))
let eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) = | false | null | false | let x = logxor a b in
let minus_x = minus x in
let x_or_minus_x = logor x minus_x in
let xnx = shift_right x_or_minus_x n_minus_one in
let c = sub_mod xnx (uint_to_t 1) in
if a = b
then
(logxor_self (v a);
lognot_lemma_1 #n;
logor_lemma_1 (v x);
assert (v x = 0 /\ v minus_x = 0 /\ v x_or_minus_x = 0 /\ v xnx = 0);
assert (v c = ones n))
else
(logxor_neq_nonzero (v a) (v b);
lemma_msb_pow2 #n (v (lognot x));
lemma_msb_pow2 #n (v minus_x);
lemma_minus_zero #n (v x);
assert (v c = FStar.UInt.zero n));
c | {
"checked_file": "FStar.UInt64.fsti.checked",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked"
],
"interface_file": false,
"source_file": "FStar.UInt64.fsti"
} | [] | [
"FStar.UInt64.t",
"Prims.unit",
"Prims.op_Equality",
"Prims._assert",
"Prims.b2t",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt.ones",
"Prims.l_and",
"Prims.int",
"FStar.UInt.logor_lemma_1",
"FStar.UInt.lognot_lemma_1",
"FStar.UInt.logxor_self",
"Prims.bool",
"FStar.UInt.zero",
"FStar.UInt.lemma_minus_zero",
"FStar.UInt.lemma_msb_pow2",
"FStar.UInt64.lognot",
"FStar.UInt.logxor_neq_nonzero",
"FStar.UInt64.sub_mod",
"FStar.UInt64.uint_to_t",
"FStar.UInt64.shift_right",
"FStar.UInt64.n_minus_one",
"FStar.UInt64.logor",
"FStar.UInt64.minus",
"FStar.UInt64.logxor",
"Prims.l_True",
"Prims.l_imp",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_disEquality"
] | [] | (*
Copyright 2008-2019 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module FStar.UInt64
(**** THIS MODULE IS GENERATED AUTOMATICALLY USING [mk_int.sh], DO NOT EDIT DIRECTLY ****)
unfold let n = 64
/// For FStar.UIntN.fstp: anything that you fix/update here should be
/// reflected in [FStar.IntN.fstp], which is mostly a copy-paste of
/// this module.
///
/// Except, as compared to [FStar.IntN.fstp], here:
/// - every occurrence of [int_t] has been replaced with [uint_t]
/// - every occurrence of [@%] has been replaced with [%].
/// - some functions (e.g., add_underspec, etc.) are only defined here, not on signed integers
/// This module provides an abstract type for machine integers of a
/// given signedness and width. The interface is designed to be safe
/// with respect to arithmetic underflow and overflow.
/// Note, we have attempted several times to re-design this module to
/// make it more amenable to normalization and to impose less overhead
/// on the SMT solver when reasoning about machine integer
/// arithmetic. The following github issue reports on the current
/// status of that work.
///
/// https://github.com/FStarLang/FStar/issues/1757
open FStar.UInt
open FStar.Mul
#set-options "--max_fuel 0 --max_ifuel 0"
(** Abstract type of machine integers, with an underlying
representation using a bounded mathematical integer *)
new val t : eqtype
(** A coercion that projects a bounded mathematical integer from a
machine integer *)
val v (x:t) : Tot (uint_t n)
(** A coercion that injects a bounded mathematical integers into a
machine integer *)
val uint_to_t (x:uint_t n) : Pure t
(requires True)
(ensures (fun y -> v y = x))
(** Injection/projection inverse *)
val uv_inv (x : t) : Lemma
(ensures (uint_to_t (v x) == x))
[SMTPat (v x)]
(** Projection/injection inverse *)
val vu_inv (x : uint_t n) : Lemma
(ensures (v (uint_to_t x) == x))
[SMTPat (uint_to_t x)]
(** An alternate form of the injectivity of the [v] projection *)
val v_inj (x1 x2: t): Lemma
(requires (v x1 == v x2))
(ensures (x1 == x2))
(** Constants 0 and 1 *)
val zero : x:t{v x = 0}
val one : x:t{v x = 1}
(**** Addition primitives *)
(** Bounds-respecting addition
The precondition enforces that the sum does not overflow,
expressing the bound as an addition on mathematical integers *)
val add (a:t) (b:t) : Pure t
(requires (size (v a + v b) n))
(ensures (fun c -> v a + v b = v c))
(** Underspecified, possibly overflowing addition:
The postcondition only enures that the result is the sum of the
arguments in case there is no overflow *)
val add_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
(** Addition modulo [2^n]
Machine integers can always be added, but the postcondition is now
in terms of addition modulo [2^n] on mathematical integers *)
val add_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.add_mod (v a) (v b) = v c))
(**** Subtraction primitives *)
(** Bounds-respecting subtraction
The precondition enforces that the difference does not underflow,
expressing the bound as a difference on mathematical integers *)
val sub (a:t) (b:t) : Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
(** Underspecified, possibly overflowing subtraction:
The postcondition only enures that the result is the difference of
the arguments in case there is no underflow *)
val sub_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
(** Subtraction modulo [2^n]
Machine integers can always be subtractd, but the postcondition is
now in terms of subtraction modulo [2^n] on mathematical integers *)
val sub_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.sub_mod (v a) (v b) = v c))
(**** Multiplication primitives *)
(** Bounds-respecting multiplication
The precondition enforces that the product does not overflow,
expressing the bound as a product on mathematical integers *)
val mul (a:t) (b:t) : Pure t
(requires (size (v a * v b) n))
(ensures (fun c -> v a * v b = v c))
(** Underspecified, possibly overflowing product
The postcondition only enures that the result is the product of
the arguments in case there is no overflow *)
val mul_underspec (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c ->
size (v a * v b) n ==> v a * v b = v c))
(** Multiplication modulo [2^n]
Machine integers can always be multiplied, but the postcondition
is now in terms of product modulo [2^n] on mathematical integers *)
val mul_mod (a:t) (b:t) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mul_mod (v a) (v b) = v c))
(**** Division primitives *)
(** Euclidean division of [a] and [b], with [b] non-zero *)
val div (a:t) (b:t{v b <> 0}) : Pure t
(requires (True))
(ensures (fun c -> v a / v b = v c))
(**** Modulo primitives *)
(** Euclidean remainder
The result is the modulus of [a] with respect to a non-zero [b] *)
val rem (a:t) (b:t{v b <> 0}) : Pure t
(requires True)
(ensures (fun c -> FStar.UInt.mod (v a) (v b) = v c))
(**** Bitwise operators *)
/// Also see FStar.BV
(** Bitwise logical conjunction *)
val logand (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logand` v y = v z))
(** Bitwise logical exclusive-or *)
val logxor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logxor` v y == v z))
(** Bitwise logical disjunction *)
val logor (x:t) (y:t) : Pure t
(requires True)
(ensures (fun z -> v x `logor` v y == v z))
(** Bitwise logical negation *)
val lognot (x:t) : Pure t
(requires True)
(ensures (fun z -> lognot (v x) == v z))
(**** Shift operators *)
(** Shift right with zero fill, shifting at most the integer width *)
val shift_right (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_right (v a) (UInt32.v s) = v c))
(** Shift left with zero fill, shifting at most the integer width *)
val shift_left (a:t) (s:UInt32.t) : Pure t
(requires (UInt32.v s < n))
(ensures (fun c -> FStar.UInt.shift_left (v a) (UInt32.v s) = v c))
(**** Comparison operators *)
(** Equality
Note, it is safe to also use the polymorphic decidable equality
operator [=] *)
let eq (a:t) (b:t) : Tot bool = eq #n (v a) (v b)
(** Greater than *)
let gt (a:t) (b:t) : Tot bool = gt #n (v a) (v b)
(** Greater than or equal *)
let gte (a:t) (b:t) : Tot bool = gte #n (v a) (v b)
(** Less than *)
let lt (a:t) (b:t) : Tot bool = lt #n (v a) (v b)
(** Less than or equal *)
let lte (a:t) (b:t) : Tot bool = lte #n (v a) (v b)
(** Unary negation *)
inline_for_extraction
let minus (a:t) = add_mod (lognot a) (uint_to_t 1)
(** The maximum value for this type *)
inline_for_extraction
let n_minus_one = UInt32.uint_to_t (n - 1)
#set-options "--z3rlimit 80 --initial_fuel 1 --max_fuel 1"
(** A constant-time way to compute the equality of
two machine integers.
With inspiration from https://git.zx2c4.com/WireGuard/commit/src/crypto/curve25519-hacl64.h?id=2e60bb395c1f589a398ec606d611132ef9ef764b
Note, the branching on [a=b] is just for proof-purposes.
*)
[@ CNoInline ]
let eq_mask (a:t) (b:t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ | false | false | FStar.UInt64.fsti | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 1,
"initial_ifuel": 1,
"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": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 80,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_mask (a b: t)
: Pure t
(requires True)
(ensures (fun c -> (v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0))) | [] | FStar.UInt64.eq_mask | {
"file_name": "ulib/FStar.UInt64.fsti",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 5,
"end_line": 282,
"start_col": 3,
"start_line": 260
} |
Prims.Tot | val wrap_slice (#a: Type0) (s: Seq.seq a) (i: int) : Seq.seq a | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0) | val wrap_slice (#a: Type0) (s: Seq.seq a) (i: int) : Seq.seq a
let wrap_slice (#a: Type0) (s: Seq.seq a) (i: int) : Seq.seq a = | false | null | false | Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0) | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"total"
] | [
"FStar.Seq.Base.seq",
"FStar.Integers.int",
"FStar.Seq.Base.slice",
"Prims.op_AmpAmp",
"FStar.Integers.op_Less_Equals",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Seq.Base.length",
"Prims.bool",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 0,
"max_fuel": 1,
"max_ifuel": 1,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": true,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "wrapped",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [
"smt.arith.nl=false",
"smt.QI.EAGER_THRESHOLD=100",
"smt.CASE_SPLIT=3"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val wrap_slice (#a: Type0) (s: Seq.seq a) (i: int) : Seq.seq a | [] | Vale.Wrapper.X64.GCMencryptOpt256.wrap_slice | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | s: FStar.Seq.Base.seq a -> i: FStar.Integers.int -> FStar.Seq.Base.seq a | {
"end_col": 62,
"end_line": 18,
"start_col": 2,
"start_line": 18
} |
FStar.Pervasives.Lemma | val length_div (b: uint8_p)
: Lemma (requires B.length b = 16) (ensures DV.length (get_downview b) / 16 = 1) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b) | val length_div (b: uint8_p)
: Lemma (requires B.length b = 16) (ensures DV.length (get_downview b) / 16 = 1)
let length_div (b: uint8_p)
: Lemma (requires B.length b = 16) (ensures DV.length (get_downview b) / 16 = 1) = | false | null | true | DV.length_eq (get_downview b) | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"LowStar.BufferView.Down.length_eq",
"FStar.UInt8.t",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"LowStar.Buffer.trivial_preorder",
"Prims.unit",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"LowStar.Monotonic.Buffer.length",
"Prims.squash",
"FStar.Integers.op_Slash",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.BufferView.Down.length",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16) | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val length_div (b: uint8_p)
: Lemma (requires B.length b = 16) (ensures DV.length (get_downview b) / 16 = 1) | [] | Vale.Wrapper.X64.GCMencryptOpt256.length_div | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p
-> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.length b = 16)
(ensures LowStar.BufferView.Down.length (Vale.Interop.Types.get_downview b) / 16 = 1) | {
"end_col": 33,
"end_line": 27,
"start_col": 4,
"start_line": 27
} |
FStar.Pervasives.Lemma | val math_cast_aux (n: UInt64.t)
: Lemma (requires UInt64.v n < pow2 32) (ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32) | val math_cast_aux (n: UInt64.t)
: Lemma (requires UInt64.v n < pow2 32) (ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
let math_cast_aux (n: UInt64.t)
: Lemma (requires UInt64.v n < pow2 32) (ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n) = | false | null | true | FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32) | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.Math.Lemmas.small_mod",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.unit",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"Prims.squash",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt32.n",
"FStar.UInt64.n",
"FStar.UInt32.v",
"FStar.Int.Cast.uint64_to_uint32",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32) | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val math_cast_aux (n: UInt64.t)
: Lemma (requires UInt64.v n < pow2 32) (ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n) | [] | Vale.Wrapper.X64.GCMencryptOpt256.math_cast_aux | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | n: FStar.UInt64.t
-> FStar.Pervasives.Lemma (requires FStar.UInt64.v n < Prims.pow2 32)
(ensures FStar.UInt32.v (FStar.Int.Cast.uint64_to_uint32 n) = FStar.UInt64.v n) | {
"end_col": 54,
"end_line": 521,
"start_col": 4,
"start_line": 521
} |
FStar.Pervasives.Lemma | val length_aux6 (b: uint8_p) : Lemma (B.length b = DV.length (get_downview b)) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let length_aux6 (b:uint8_p) : Lemma (B.length b = DV.length (get_downview b))
= DV.length_eq (get_downview b) | val length_aux6 (b: uint8_p) : Lemma (B.length b = DV.length (get_downview b))
let length_aux6 (b: uint8_p) : Lemma (B.length b = DV.length (get_downview b)) = | false | null | true | DV.length_eq (get_downview b) | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"LowStar.BufferView.Down.length_eq",
"FStar.UInt8.t",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"LowStar.Buffer.trivial_preorder",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"LowStar.BufferView.Down.length",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32)
inline_for_extraction
let gcm256_encrypt_opt_alloca key iv plain_b plain_len auth_b auth_bytes iv_b
out_b tag_b keys_b hkeys_b scratch_b inout_b abytes_b =
let h0 = get() in
// Simplify the expression for the iv
DV.length_eq (get_downview iv_b);
length_aux4 iv_b;
calc (==) {
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))
(Ghost.reveal iv);
(==) { }
le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b));
(==) { gcm_simplify2 iv_b h0 }
le_bytes_to_quad32 (le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0));
(==) { le_bytes_to_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0) }
low_buffer_read TUInt8 TUInt128 h0 iv_b 0;
};
let lemma_uv_key () : Lemma
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key)))
= length_aux2 keys_b;
let db = get_downview keys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
le_bytes_to_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key));
assert (Seq.equal (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b)))
(key_to_round_keys_LE AES_256 (Ghost.reveal key)));
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 keys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_key ();
// Simplify the precondition for hkeys_b
let lemma_uv_hkey () : Lemma
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
UV.as_seq h0 ub == le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
= length_aux5 hkeys_b;
let db = get_downview hkeys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 hkeys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_hkey ();
// Compute length of biggest blocks of 6 * 128-bit blocks
let len128x6 = UInt64.mul (plain_len / 96uL) 96uL in
if len128x6 / 16uL >= 18uL then (
let len128_num = ((plain_len / 16uL) * 16uL) - len128x6 in
// Casting to uint32 is here the equality
math_cast_aux len128x6;
math_cast_aux len128_num;
let in128x6_b = B.sub plain_b 0ul (uint64_to_uint32 len128x6) in
let out128x6_b = B.sub out_b 0ul (uint64_to_uint32 len128x6) in
let in128_b = B.sub plain_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128x6' = UInt64.div len128x6 16uL in
let len128_num' = UInt64.div len128_num 16uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b (uint64_to_uint32 len128x6);
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b (uint64_to_uint32 len128x6);
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
) else (
let len128x6 = 0ul in
// Compute the size of the remaining 128-bit blocks
let len128_num = ((plain_len / 16uL) * 16uL) in
// Casting to uint32 is here the equality
FStar.Math.Lemmas.small_mod (UInt64.v len128_num) pow2_32;
let in128x6_b = B.sub plain_b 0ul len128x6 in
let out128x6_b = B.sub out_b 0ul len128x6 in
let in128_b = B.sub plain_b len128x6 (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b len128x6 (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128_num' = UInt64.div len128_num 16uL in
let len128x6' = 0uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b len128x6;
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b len128x6;
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
);
// Simplify post condition for tag
let h_f = get() in
gcm_simplify2 tag_b h_f
let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
= lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1 | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val length_aux6 (b: uint8_p) : Lemma (B.length b = DV.length (get_downview b)) | [] | Vale.Wrapper.X64.GCMencryptOpt256.length_aux6 | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p
-> FStar.Pervasives.Lemma
(ensures
LowStar.Monotonic.Buffer.length b =
LowStar.BufferView.Down.length (Vale.Interop.Types.get_downview b)) | {
"end_col": 33,
"end_line": 762,
"start_col": 4,
"start_line": 762
} |
FStar.Pervasives.Lemma | val lemma_same_seq_dv (h: HS.mem) (b: uint8_p)
: Lemma (Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux | val lemma_same_seq_dv (h: HS.mem) (b: uint8_p)
: Lemma (Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b)))
let lemma_same_seq_dv (h: HS.mem) (b: uint8_p)
: Lemma (Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) = | false | null | true | let db = get_downview b in
DV.length_eq db;
let aux (i: nat{i < B.length b})
: Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in
Classical.forall_intro aux | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"FStar.Monotonic.HyperStack.mem",
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"FStar.Classical.forall_intro",
"FStar.Integers.nat",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.Monotonic.Buffer.length",
"FStar.UInt8.t",
"LowStar.Buffer.trivial_preorder",
"Prims.eq2",
"FStar.Seq.Base.index",
"LowStar.Monotonic.Buffer.as_seq",
"LowStar.BufferView.Down.as_seq",
"FStar.Integers.int_t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_LessThan",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Vale.Interop.Views.put8_reveal",
"LowStar.BufferView.Down.get_sel",
"LowStar.BufferView.Down.as_seq_sel",
"LowStar.BufferView.Down.length_eq",
"LowStar.BufferView.Down.buffer",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"FStar.Seq.Base.equal"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
)) | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_same_seq_dv (h: HS.mem) (b: uint8_p)
: Lemma (Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) | [] | Vale.Wrapper.X64.GCMencryptOpt256.lemma_same_seq_dv | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | h: FStar.Monotonic.HyperStack.mem -> b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.equal (LowStar.Monotonic.Buffer.as_seq h b)
(LowStar.BufferView.Down.as_seq h (Vale.Interop.Types.get_downview b))) | {
"end_col": 31,
"end_line": 424,
"start_col": 61,
"start_line": 417
} |
FStar.Pervasives.Lemma | val lemma_identical_uv (b: uint8_p) (h0 h1: HS.mem)
: Lemma (requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures
(let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u))) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
= lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1 | val lemma_identical_uv (b: uint8_p) (h0 h1: HS.mem)
: Lemma (requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures
(let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
let lemma_identical_uv (b: uint8_p) (h0 h1: HS.mem)
: Lemma (requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures
(let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u))) = | false | null | true | lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1 | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"FStar.Monotonic.HyperStack.mem",
"Vale.Lib.BufferViewHelpers.lemma_uv_equal",
"FStar.UInt8.t",
"Vale.Def.Types_s.quad32",
"Vale.Interop.Views.up_view128",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"LowStar.Buffer.trivial_preorder",
"Prims.unit",
"Vale.Wrapper.X64.GCMencryptOpt256.length_aux3",
"FStar.Integers.op_Slash",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"LowStar.Monotonic.Buffer.length",
"Vale.Lib.BufferViewHelpers.lemma_dv_equal",
"Vale.Interop.Views.down_view8",
"Prims.l_and",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.Integers.op_Percent",
"FStar.Seq.Base.equal",
"LowStar.Monotonic.Buffer.as_seq",
"Prims.squash",
"LowStar.BufferView.Up.as_seq",
"LowStar.BufferView.Up.buffer",
"LowStar.BufferView.Up.mk_buffer",
"LowStar.BufferView.Down.buffer",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32)
inline_for_extraction
let gcm256_encrypt_opt_alloca key iv plain_b plain_len auth_b auth_bytes iv_b
out_b tag_b keys_b hkeys_b scratch_b inout_b abytes_b =
let h0 = get() in
// Simplify the expression for the iv
DV.length_eq (get_downview iv_b);
length_aux4 iv_b;
calc (==) {
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))
(Ghost.reveal iv);
(==) { }
le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b));
(==) { gcm_simplify2 iv_b h0 }
le_bytes_to_quad32 (le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0));
(==) { le_bytes_to_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0) }
low_buffer_read TUInt8 TUInt128 h0 iv_b 0;
};
let lemma_uv_key () : Lemma
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key)))
= length_aux2 keys_b;
let db = get_downview keys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
le_bytes_to_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key));
assert (Seq.equal (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b)))
(key_to_round_keys_LE AES_256 (Ghost.reveal key)));
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 keys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_key ();
// Simplify the precondition for hkeys_b
let lemma_uv_hkey () : Lemma
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
UV.as_seq h0 ub == le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
= length_aux5 hkeys_b;
let db = get_downview hkeys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 hkeys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_hkey ();
// Compute length of biggest blocks of 6 * 128-bit blocks
let len128x6 = UInt64.mul (plain_len / 96uL) 96uL in
if len128x6 / 16uL >= 18uL then (
let len128_num = ((plain_len / 16uL) * 16uL) - len128x6 in
// Casting to uint32 is here the equality
math_cast_aux len128x6;
math_cast_aux len128_num;
let in128x6_b = B.sub plain_b 0ul (uint64_to_uint32 len128x6) in
let out128x6_b = B.sub out_b 0ul (uint64_to_uint32 len128x6) in
let in128_b = B.sub plain_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128x6' = UInt64.div len128x6 16uL in
let len128_num' = UInt64.div len128_num 16uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b (uint64_to_uint32 len128x6);
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b (uint64_to_uint32 len128x6);
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
) else (
let len128x6 = 0ul in
// Compute the size of the remaining 128-bit blocks
let len128_num = ((plain_len / 16uL) * 16uL) in
// Casting to uint32 is here the equality
FStar.Math.Lemmas.small_mod (UInt64.v len128_num) pow2_32;
let in128x6_b = B.sub plain_b 0ul len128x6 in
let out128x6_b = B.sub out_b 0ul len128x6 in
let in128_b = B.sub plain_b len128x6 (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b len128x6 (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128_num' = UInt64.div len128_num 16uL in
let len128x6' = 0uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b len128x6;
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b len128x6;
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
);
// Simplify post condition for tag
let h_f = get() in
gcm_simplify2 tag_b h_f
let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_identical_uv (b: uint8_p) (h0 h1: HS.mem)
: Lemma (requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures
(let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u))) | [] | Vale.Wrapper.X64.GCMencryptOpt256.lemma_identical_uv | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
h0: FStar.Monotonic.HyperStack.mem ->
h1: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Monotonic.Buffer.length b % 16 = 0 /\
FStar.Seq.Base.equal (LowStar.Monotonic.Buffer.as_seq h0 b)
(LowStar.Monotonic.Buffer.as_seq h1 b))
(ensures
(let b_d = Vale.Interop.Types.get_downview b in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux3 b (LowStar.Monotonic.Buffer.length b / 16)
in
let b_u = LowStar.BufferView.Up.mk_buffer b_d Vale.Interop.Views.up_view128 in
FStar.Seq.Base.equal (LowStar.BufferView.Up.as_seq h0 b_u)
(LowStar.BufferView.Up.as_seq h1 b_u))) | {
"end_col": 71,
"end_line": 758,
"start_col": 4,
"start_line": 756
} |
FStar.Pervasives.Lemma | val lemma_slice_uv_extra (b b_start b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_start = (B.length b / 16) * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\ B.length b_extra = 16 /\
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b)))
(ensures
(let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b)))) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let lemma_slice_uv_extra (b:uint8_p) (b_start:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires
B.length b_start = B.length b / 16 * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\
B.length b_extra = 16 /\
Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b))
)
(ensures (
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b))
))
=
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
let b_f = seq_uint8_to_seq_nat8 (B.as_seq h b) in
// if B.length b > B.length b_start then (
calc (==) {
sf;
(==) { DV.length_eq b_start_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_start h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_start_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(UV.as_seq h b_extra_u)))
(B.length b);
(==) { DV.length_eq b_extra_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_extra h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_extra_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { append_distributes_le_seq_quad32_to_bytes
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
}
wrap_slice (Seq.append
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start))))
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start));
le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)) }
wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b);
(==) { Seq.lemma_eq_intro b_f
(wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b))
}
b_f;
} | val lemma_slice_uv_extra (b b_start b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_start = (B.length b / 16) * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\ B.length b_extra = 16 /\
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b)))
(ensures
(let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b))))
let lemma_slice_uv_extra (b b_start b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_start = (B.length b / 16) * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\ B.length b_extra = 16 /\
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b)))
(ensures
(let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b)))) = | false | null | true | let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
let b_f = seq_uint8_to_seq_nat8 (B.as_seq h b) in
calc ( == ) {
sf;
( == ) { (DV.length_eq b_start_d;
lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_start h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_start_u)) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq
h
b_start)))
(UV.as_seq h b_extra_u)))
(B.length b);
( == ) { (DV.length_eq b_extra_d;
lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_extra h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_extra_u)) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq
h
b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
( == ) { append_distributes_le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (
B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra))) }
wrap_slice (Seq.append (le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq
h
b_start))))
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra))
)))
(B.length b);
( == ) { (le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start));
le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra))) }
wrap_slice (Seq.append (seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b);
( == ) { Seq.lemma_eq_intro b_f
(wrap_slice (Seq.append (seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b)) }
b_f;
} | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"FStar.Monotonic.HyperStack.mem",
"FStar.Calc.calc_finish",
"FStar.Seq.Base.seq",
"Vale.Def.Types_s.nat8",
"Prims.eq2",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"Vale.Wrapper.X64.GCMencryptOpt256.wrap_slice",
"Vale.Def.Words_s.nat8",
"FStar.Seq.Base.append",
"Vale.Def.Words.Seq_s.seq_uint8_to_seq_nat8",
"LowStar.Monotonic.Buffer.as_seq",
"FStar.UInt8.t",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.length",
"Vale.Def.Types_s.le_seq_quad32_to_bytes",
"Vale.Def.Types_s.le_bytes_to_seq_quad32",
"Vale.Def.Types_s.quad32",
"LowStar.BufferView.Up.as_seq",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Vale.Arch.Types.le_bytes_to_seq_quad32_to_bytes",
"Vale.SHA.Simplify_Sha.lemma_seq_nat8_le_seq_quad32_to_bytes_uint32",
"LowStar.BufferView.Down.length_eq",
"Prims.squash",
"Vale.Arch.Types.append_distributes_le_seq_quad32_to_bytes",
"Vale.Arch.Types.le_seq_quad32_to_bytes_to_seq_quad32",
"FStar.Seq.Base.lemma_eq_intro",
"LowStar.BufferView.Up.buffer",
"LowStar.BufferView.Up.mk_buffer",
"Vale.Interop.Views.up_view128",
"Vale.Wrapper.X64.GCMencryptOpt256.length_aux6",
"LowStar.BufferView.Down.buffer",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"Prims.l_and",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.Integers.op_Star",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Integers.op_Slash",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.gsub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.uint_to_t",
"FStar.Seq.Base.equal",
"FStar.Seq.Base.slice",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32)
inline_for_extraction
let gcm256_encrypt_opt_alloca key iv plain_b plain_len auth_b auth_bytes iv_b
out_b tag_b keys_b hkeys_b scratch_b inout_b abytes_b =
let h0 = get() in
// Simplify the expression for the iv
DV.length_eq (get_downview iv_b);
length_aux4 iv_b;
calc (==) {
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))
(Ghost.reveal iv);
(==) { }
le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b));
(==) { gcm_simplify2 iv_b h0 }
le_bytes_to_quad32 (le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0));
(==) { le_bytes_to_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0) }
low_buffer_read TUInt8 TUInt128 h0 iv_b 0;
};
let lemma_uv_key () : Lemma
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key)))
= length_aux2 keys_b;
let db = get_downview keys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
le_bytes_to_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key));
assert (Seq.equal (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b)))
(key_to_round_keys_LE AES_256 (Ghost.reveal key)));
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 keys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_key ();
// Simplify the precondition for hkeys_b
let lemma_uv_hkey () : Lemma
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
UV.as_seq h0 ub == le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
= length_aux5 hkeys_b;
let db = get_downview hkeys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 hkeys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_hkey ();
// Compute length of biggest blocks of 6 * 128-bit blocks
let len128x6 = UInt64.mul (plain_len / 96uL) 96uL in
if len128x6 / 16uL >= 18uL then (
let len128_num = ((plain_len / 16uL) * 16uL) - len128x6 in
// Casting to uint32 is here the equality
math_cast_aux len128x6;
math_cast_aux len128_num;
let in128x6_b = B.sub plain_b 0ul (uint64_to_uint32 len128x6) in
let out128x6_b = B.sub out_b 0ul (uint64_to_uint32 len128x6) in
let in128_b = B.sub plain_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128x6' = UInt64.div len128x6 16uL in
let len128_num' = UInt64.div len128_num 16uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b (uint64_to_uint32 len128x6);
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b (uint64_to_uint32 len128x6);
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
) else (
let len128x6 = 0ul in
// Compute the size of the remaining 128-bit blocks
let len128_num = ((plain_len / 16uL) * 16uL) in
// Casting to uint32 is here the equality
FStar.Math.Lemmas.small_mod (UInt64.v len128_num) pow2_32;
let in128x6_b = B.sub plain_b 0ul len128x6 in
let out128x6_b = B.sub out_b 0ul len128x6 in
let in128_b = B.sub plain_b len128x6 (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b len128x6 (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128_num' = UInt64.div len128_num 16uL in
let len128x6' = 0uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b len128x6;
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b len128x6;
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
);
// Simplify post condition for tag
let h_f = get() in
gcm_simplify2 tag_b h_f
let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
= lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1
let length_aux6 (b:uint8_p) : Lemma (B.length b = DV.length (get_downview b))
= DV.length_eq (get_downview b)
#push-options "--z3cliopt smt.arith.nl=true"
let lemma_slice_uv_extra (b:uint8_p) (b_start:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires
B.length b_start = B.length b / 16 * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\
B.length b_extra = 16 /\
Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b))
)
(ensures (
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b)) | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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",
"smt.arith.nl=true"
],
"z3refresh": false,
"z3rlimit": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_slice_uv_extra (b b_start b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_start = (B.length b / 16) * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\ B.length b_extra = 16 /\
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b)))
(ensures
(let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b)))) | [] | Vale.Wrapper.X64.GCMencryptOpt256.lemma_slice_uv_extra | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
b_start: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
b_extra: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Monotonic.Buffer.length b_start = (LowStar.Monotonic.Buffer.length b / 16) * 16 /\
b_start ==
LowStar.Buffer.gsub b 0ul (FStar.UInt32.uint_to_t (LowStar.Monotonic.Buffer.length b_start)) /\
LowStar.Monotonic.Buffer.length b_extra = 16 /\
FStar.Seq.Base.equal (LowStar.Monotonic.Buffer.as_seq h b)
(FStar.Seq.Base.slice (FStar.Seq.Base.append (LowStar.Monotonic.Buffer.as_seq h b_start)
(LowStar.Monotonic.Buffer.as_seq h b_extra))
0
(LowStar.Monotonic.Buffer.length b)))
(ensures
(let b_start_d = Vale.Interop.Types.get_downview b_start in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux6 b_start
in
let b_start_u = LowStar.BufferView.Up.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = Vale.Interop.Types.get_downview b_extra in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux6 b_extra
in
let b_extra_u = LowStar.BufferView.Up.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv =
FStar.Seq.Base.append (LowStar.BufferView.Up.as_seq h b_start_u)
(LowStar.BufferView.Up.as_seq h b_extra_u)
in
let sf =
Vale.Wrapper.X64.GCMencryptOpt256.wrap_slice (Vale.Def.Types_s.le_seq_quad32_to_bytes suv
)
(LowStar.Monotonic.Buffer.length b)
in
FStar.Seq.Base.equal sf
(Vale.Def.Words.Seq_s.seq_uint8_to_seq_nat8 (LowStar.Monotonic.Buffer.as_seq h b)))) | {
"end_col": 4,
"end_line": 831,
"start_col": 2,
"start_line": 785
} |
FStar.Pervasives.Lemma | val lemma_slice_sub (b b_sub b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_extra = 16 /\ B.length b_sub = (B.length b / 16) * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal (Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
(ensures
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let lemma_slice_sub (b:uint8_p) (b_sub:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires B.length b_extra = 16 /\ B.length b_sub = B.length b / 16 * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16))
)
(ensures Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))
) =
calc (==) {
Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b);
(==) { Seq.lemma_eq_intro
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))
(Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
}
Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
(==) { }
Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
(==) { }
Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b));
(==) { Seq.lemma_eq_intro (B.as_seq h b)
(Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b)))
}
B.as_seq h b;
} | val lemma_slice_sub (b b_sub b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_extra = 16 /\ B.length b_sub = (B.length b / 16) * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal (Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
(ensures
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b)))
let lemma_slice_sub (b b_sub b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_extra = 16 /\ B.length b_sub = (B.length b / 16) * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal (Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
(ensures
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))) = | false | null | true | calc ( == ) {
Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b);
( == ) { Seq.lemma_eq_intro (Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra))
0
(B.length b))
(Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16))) }
Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
( == ) { () }
Seq.append (Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
( == ) { () }
Seq.append (Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b));
( == ) { Seq.lemma_eq_intro (B.as_seq h b)
(Seq.append (Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b))) }
B.as_seq h b;
} | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"FStar.Monotonic.HyperStack.mem",
"FStar.Calc.calc_finish",
"FStar.Seq.Base.seq",
"FStar.UInt8.t",
"Prims.eq2",
"FStar.Seq.Base.slice",
"FStar.Seq.Base.append",
"LowStar.Monotonic.Buffer.as_seq",
"LowStar.Buffer.trivial_preorder",
"LowStar.Monotonic.Buffer.length",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"FStar.Integers.op_Percent",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"FStar.Seq.Base.lemma_eq_intro",
"Prims.squash",
"Prims.l_and",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.Integers.op_Star",
"FStar.Integers.op_Slash",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.gsub",
"FStar.UInt32.__uint_to_t",
"FStar.UInt32.uint_to_t",
"FStar.Seq.Base.equal",
"FStar.Integers.op_Plus",
"FStar.Pervasives.pattern"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32)
inline_for_extraction
let gcm256_encrypt_opt_alloca key iv plain_b plain_len auth_b auth_bytes iv_b
out_b tag_b keys_b hkeys_b scratch_b inout_b abytes_b =
let h0 = get() in
// Simplify the expression for the iv
DV.length_eq (get_downview iv_b);
length_aux4 iv_b;
calc (==) {
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))
(Ghost.reveal iv);
(==) { }
le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b));
(==) { gcm_simplify2 iv_b h0 }
le_bytes_to_quad32 (le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0));
(==) { le_bytes_to_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0) }
low_buffer_read TUInt8 TUInt128 h0 iv_b 0;
};
let lemma_uv_key () : Lemma
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key)))
= length_aux2 keys_b;
let db = get_downview keys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
le_bytes_to_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key));
assert (Seq.equal (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b)))
(key_to_round_keys_LE AES_256 (Ghost.reveal key)));
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 keys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_key ();
// Simplify the precondition for hkeys_b
let lemma_uv_hkey () : Lemma
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
UV.as_seq h0 ub == le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
= length_aux5 hkeys_b;
let db = get_downview hkeys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 hkeys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_hkey ();
// Compute length of biggest blocks of 6 * 128-bit blocks
let len128x6 = UInt64.mul (plain_len / 96uL) 96uL in
if len128x6 / 16uL >= 18uL then (
let len128_num = ((plain_len / 16uL) * 16uL) - len128x6 in
// Casting to uint32 is here the equality
math_cast_aux len128x6;
math_cast_aux len128_num;
let in128x6_b = B.sub plain_b 0ul (uint64_to_uint32 len128x6) in
let out128x6_b = B.sub out_b 0ul (uint64_to_uint32 len128x6) in
let in128_b = B.sub plain_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128x6' = UInt64.div len128x6 16uL in
let len128_num' = UInt64.div len128_num 16uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b (uint64_to_uint32 len128x6);
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b (uint64_to_uint32 len128x6);
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
) else (
let len128x6 = 0ul in
// Compute the size of the remaining 128-bit blocks
let len128_num = ((plain_len / 16uL) * 16uL) in
// Casting to uint32 is here the equality
FStar.Math.Lemmas.small_mod (UInt64.v len128_num) pow2_32;
let in128x6_b = B.sub plain_b 0ul len128x6 in
let out128x6_b = B.sub out_b 0ul len128x6 in
let in128_b = B.sub plain_b len128x6 (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b len128x6 (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128_num' = UInt64.div len128_num 16uL in
let len128x6' = 0uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b len128x6;
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b len128x6;
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
);
// Simplify post condition for tag
let h_f = get() in
gcm_simplify2 tag_b h_f
let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
= lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1
let length_aux6 (b:uint8_p) : Lemma (B.length b = DV.length (get_downview b))
= DV.length_eq (get_downview b)
#push-options "--z3cliopt smt.arith.nl=true"
let lemma_slice_uv_extra (b:uint8_p) (b_start:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires
B.length b_start = B.length b / 16 * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\
B.length b_extra = 16 /\
Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b))
)
(ensures (
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b))
))
=
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
let b_f = seq_uint8_to_seq_nat8 (B.as_seq h b) in
// if B.length b > B.length b_start then (
calc (==) {
sf;
(==) { DV.length_eq b_start_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_start h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_start_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(UV.as_seq h b_extra_u)))
(B.length b);
(==) { DV.length_eq b_extra_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_extra h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_extra_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { append_distributes_le_seq_quad32_to_bytes
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
}
wrap_slice (Seq.append
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start))))
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start));
le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)) }
wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b);
(==) { Seq.lemma_eq_intro b_f
(wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b))
}
b_f;
}
// ) else (
// calc (==) {
// sf;
// (==) { }
// wrap_slice (le_seq_quad32_to_bytes (UV.as_seq h b_start_u)) (B.length b);
// (==) { DV.length_eq (get_downview b_start);
// gcm_simplify1 b_start h (B.length b) }
// seq_uint8_to_seq_nat8 (B.as_seq h b_start);
// (==) { }
// b_f;
// }
// )
#pop-options
let lemma_slice_sub (b:uint8_p) (b_sub:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires B.length b_extra = 16 /\ B.length b_sub = B.length b / 16 * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16))
)
(ensures Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b)) | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_slice_sub (b b_sub b_extra: uint8_p) (h: HS.mem)
: Lemma
(requires
B.length b_extra = 16 /\ B.length b_sub = (B.length b / 16) * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal (Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
(ensures
Seq.equal (B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))) | [] | Vale.Wrapper.X64.GCMencryptOpt256.lemma_slice_sub | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
b_sub: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
b_extra: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowStar.Monotonic.Buffer.length b_extra = 16 /\
LowStar.Monotonic.Buffer.length b_sub = (LowStar.Monotonic.Buffer.length b / 16) * 16 /\
b_sub ==
LowStar.Buffer.gsub b 0ul (FStar.UInt32.uint_to_t (LowStar.Monotonic.Buffer.length b_sub)) /\
FStar.Seq.Base.equal (FStar.Seq.Base.slice (LowStar.Monotonic.Buffer.as_seq h b)
(LowStar.Monotonic.Buffer.length b_sub)
(LowStar.Monotonic.Buffer.length b_sub + LowStar.Monotonic.Buffer.length b % 16))
(FStar.Seq.Base.slice (LowStar.Monotonic.Buffer.as_seq h b_extra)
0
(LowStar.Monotonic.Buffer.length b % 16)))
(ensures
FStar.Seq.Base.equal (LowStar.Monotonic.Buffer.as_seq h b)
(FStar.Seq.Base.slice (FStar.Seq.Base.append (LowStar.Monotonic.Buffer.as_seq h b_sub)
(LowStar.Monotonic.Buffer.as_seq h b_extra))
0
(LowStar.Monotonic.Buffer.length b))) | {
"end_col": 3,
"end_line": 878,
"start_col": 2,
"start_line": 857
} |
FStar.Pervasives.Lemma | val lemma_uv_split (h: HS.mem) (b: uint8_p) (n: UInt32.t)
: Lemma (requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures
(let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)) | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux | val lemma_uv_split (h: HS.mem) (b: uint8_p) (n: UInt32.t)
: Lemma (requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures
(let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs))
let lemma_uv_split (h: HS.mem) (b: uint8_p) (n: UInt32.t)
: Lemma (requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures
(let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)) = | false | null | true | let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc ( == ) {
Seq.length split_bs;
( == ) { () }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
( == ) { (UV.length_eq b1_u;
UV.length_eq b2_u) }
DV.length b1_d / 16 + DV.length b2_d / 16;
( == ) { (DV.length_eq b1_d;
DV.length_eq b2_d;
math_aux (B.length b1);
math_aux (B.length b2)) }
B.length b1 / 16 + B.length b2 / 16;
( == ) { () }
B.length b / 16;
( == ) { (DV.length_eq b_d;
UV.length_eq b_u;
math_aux (B.length b)) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i: nat{i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i) =
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc ( == ) {
Seq.index bs i;
( == ) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
( == ) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
( == ) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
( == ) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2))
(i * 16)
(i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u)
then
(lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc ( == ) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2))
(i * 16)
(i * 16 + 16));
( == ) { () }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
( == ) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
( == ) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
( == ) { () }
Seq.index split_bs i;
})
else
(lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc ( == ) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2))
(i * 16)
(i * 16 + 16));
( == ) { () }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
( == ) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
( == ) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
( == ) { () }
Seq.index split_bs i;
})
in
Classical.forall_intro aux | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"lemma"
] | [
"FStar.Monotonic.HyperStack.mem",
"Vale.Wrapper.X64.GCMencryptOpt256.uint8_p",
"FStar.UInt32.t",
"FStar.Classical.forall_intro",
"FStar.Integers.nat",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.Seq.Base.length",
"Vale.Def.Types_s.quad32",
"Prims.op_Equality",
"FStar.Seq.Base.index",
"FStar.Integers.int_t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_LessThan",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern",
"LowStar.BufferView.Up.as_seq",
"FStar.Calc.calc_finish",
"Prims.eq2",
"Vale.Interop.Views.get128",
"FStar.Seq.Base.slice",
"FStar.UInt8.t",
"FStar.Seq.Base.append",
"LowStar.Monotonic.Buffer.as_seq",
"LowStar.Buffer.trivial_preorder",
"FStar.Integers.op_Star",
"FStar.Integers.op_Plus",
"Prims.Cons",
"FStar.Preorder.relation",
"FStar.Calc.calc_step",
"LowStar.BufferView.Up.sel",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"LowStar.BufferView.Up.get_sel",
"LowStar.BufferView.Up.as_seq_sel",
"LowStar.BufferView.Up.length_eq",
"Vale.Wrapper.X64.GCMencryptOpt256.lemma_same_seq_dv",
"Prims.bool",
"FStar.Integers.op_Subtraction",
"LowStar.BufferView.Up.length",
"LowStar.BufferView.Down.as_seq",
"Prims._assert",
"FStar.Seq.Base.equal",
"Prims.nat",
"FStar.Integers.op_Slash",
"LowStar.Monotonic.Buffer.length",
"LowStar.BufferView.Down.length",
"Vale.Wrapper.X64.GCMencryptOpt256.math_aux",
"LowStar.BufferView.Down.length_eq",
"FStar.Seq.Properties.lseq",
"FStar.Seq.Base.seq",
"LowStar.BufferView.Up.buffer",
"LowStar.BufferView.Up.mk_buffer",
"Vale.Interop.Views.up_view128",
"Vale.Wrapper.X64.GCMencryptOpt256.length_aux3",
"LowStar.BufferView.Down.buffer",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.gsub",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.UInt32.uint_to_t",
"FStar.UInt32.__uint_to_t",
"Prims.l_and",
"Prims.int",
"FStar.Integers.op_Percent",
"FStar.UInt32.v",
"FStar.Integers.op_Less_Equals"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in | false | false | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 400,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lemma_uv_split (h: HS.mem) (b: uint8_p) (n: UInt32.t)
: Lemma (requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures
(let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)) | [] | Vale.Wrapper.X64.GCMencryptOpt256.lemma_uv_split | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} |
h: FStar.Monotonic.HyperStack.mem ->
b: Vale.Wrapper.X64.GCMencryptOpt256.uint8_p ->
n: FStar.UInt32.t
-> FStar.Pervasives.Lemma
(requires
LowStar.Monotonic.Buffer.length b % 16 = 0 /\ FStar.UInt32.v n % 16 = 0 /\
FStar.UInt32.v n <= LowStar.Monotonic.Buffer.length b)
(ensures
(let b1 = LowStar.Buffer.gsub b 0ul n in
let b2 =
LowStar.Buffer.gsub b n (FStar.UInt32.uint_to_t (LowStar.Monotonic.Buffer.length b) - n)
in
let b1_d = Vale.Interop.Types.get_downview b1 in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux3 b1
(LowStar.Monotonic.Buffer.length b1 / 16)
in
let b1_u = LowStar.BufferView.Up.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = Vale.Interop.Types.get_downview b2 in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux3 b2
(LowStar.Monotonic.Buffer.length b2 / 16)
in
let b2_u = LowStar.BufferView.Up.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = Vale.Interop.Types.get_downview b in
[@@ FStar.Pervasives.inline_let ]let _ =
Vale.Wrapper.X64.GCMencryptOpt256.length_aux3 b (LowStar.Monotonic.Buffer.length b / 16)
in
let b_u = LowStar.BufferView.Up.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs =
FStar.Seq.Base.append (LowStar.BufferView.Up.as_seq h b1_u)
(LowStar.BufferView.Up.as_seq h b2_u)
in
let bs = LowStar.BufferView.Up.as_seq h b_u in
FStar.Seq.Base.equal bs split_bs)) | {
"end_col": 33,
"end_line": 516,
"start_col": 5,
"start_line": 443
} |
Prims.Tot | val gcm256_encrypt_opt_stdcall: Vale.Wrapper.X64.GCMencryptOpt.encrypt_opt_stdcall_st AES_256 | [
{
"abbrev": false,
"full_module": "Vale.Lib.BufferViewHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Integers",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Int.Cast",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Calc",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.Gcm_simplify",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.SHA.Simplify_Sha",
"short_module": null
},
{
"abbrev": true,
"full_module": "Vale.X64.Decls",
"short_module": "V"
},
{
"abbrev": false,
"full_module": "Vale.X64.MemoryAdapters",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Stdcalls.X64.GCMencryptOpt",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.OptPublic",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Arch.Types",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Interop.Base",
"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.GHash_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.AES_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AES.GCM_helpers",
"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.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.AsLowStar.MemoryHelpers",
"short_module": null
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Up",
"short_module": "UV"
},
{
"abbrev": true,
"full_module": "LowStar.BufferView.Down",
"short_module": "DV"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.X64.CPU_Features_s",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.X64",
"short_module": null
},
{
"abbrev": false,
"full_module": "Vale.Wrapper.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
}
] | false | let gcm256_encrypt_opt_stdcall key iv plain_b plain_len auth_b auth_len iv_b out_b tag_b keys_b hkeys_b scratch_b =
let h0 = get() in
push_frame();
// Extra space to have a full input/output with length % 16 = 0
let inout_b = B.sub scratch_b 0ul 16ul in
// Same for auth_b
let abytes_b = B.sub scratch_b 16ul 16ul in
// Scratch space for Vale procedure
let scratch_b = B.sub scratch_b 32ul 144ul in
// Copy the remainder of plain_b into inout_b
math_cast_aux plain_len;
math_cast_aux auth_len;
let plain_len' = (uint64_to_uint32 plain_len / 16ul) * 16ul in
let auth_len' = (uint64_to_uint32 auth_len / 16ul) * 16ul in
let plain_b' = B.sub plain_b 0ul plain_len' in
let out_b' = B.sub out_b 0ul plain_len' in
let auth_b' = B.sub auth_b 0ul auth_len' in
B.blit plain_b plain_len' inout_b 0ul (uint64_to_uint32 plain_len % 16ul);
let h1 = get() in
// Same with auth_b and abytes_b
B.blit auth_b auth_len' abytes_b 0ul (uint64_to_uint32 auth_len % 16ul);
let h1 = get() in
gcm256_encrypt_opt_alloca
key
iv
plain_b'
plain_len
auth_b'
auth_len
iv_b
out_b'
tag_b
keys_b
hkeys_b
scratch_b
inout_b
abytes_b;
let h_post = get() in
// Copy back the remainder in inout_b into out_b
B.blit inout_b 0ul out_b ((uint64_to_uint32 plain_len / 16ul) * 16ul) (uint64_to_uint32 plain_len % 16ul);
let h2 = get() in
assert (
let plain_d = get_downview plain_b' in
length_aux3 plain_b' (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b' in
length_aux3 out_b' (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h1 plain_u) (UV.as_seq h1 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h_post out_u) (UV.as_seq h_post inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b' in
length_aux3 auth_b' (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h1 auth_u) (UV.as_seq h1 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h_post tag_b)) tag
));
lemma_identical_uv out_b' h_post h2;
lemma_identical_uv inout_b h_post h2;
assert (
let out_d = get_downview out_b' in
length_aux3 out_b' (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
UV.as_seq h_post out_u == UV.as_seq h2 out_u);
assert (
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
UV.as_seq h_post inout_u == UV.as_seq h2 inout_u);
lemma_slice_sub plain_b plain_b' inout_b h1;
lemma_slice_uv_extra plain_b plain_b' inout_b h1;
lemma_slice_sub auth_b auth_b' abytes_b h1;
lemma_slice_sub out_b out_b' inout_b h2;
lemma_slice_uv_extra auth_b auth_b' abytes_b h1;
lemma_slice_uv_extra out_b out_b' inout_b h2;
assert (
let plain = seq_uint8_to_seq_nat8 (B.as_seq h1 plain_b) in
let auth = seq_uint8_to_seq_nat8 (B.as_seq h1 auth_b) in
let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain auth in
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h2 out_b)) cipher /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h2 tag_b)) tag);
pop_frame() | val gcm256_encrypt_opt_stdcall: Vale.Wrapper.X64.GCMencryptOpt.encrypt_opt_stdcall_st AES_256
let gcm256_encrypt_opt_stdcall
key
iv
plain_b
plain_len
auth_b
auth_len
iv_b
out_b
tag_b
keys_b
hkeys_b
scratch_b
= | false | null | false | let h0 = get () in
push_frame ();
let inout_b = B.sub scratch_b 0ul 16ul in
let abytes_b = B.sub scratch_b 16ul 16ul in
let scratch_b = B.sub scratch_b 32ul 144ul in
math_cast_aux plain_len;
math_cast_aux auth_len;
let plain_len' = (uint64_to_uint32 plain_len / 16ul) * 16ul in
let auth_len' = (uint64_to_uint32 auth_len / 16ul) * 16ul in
let plain_b' = B.sub plain_b 0ul plain_len' in
let out_b' = B.sub out_b 0ul plain_len' in
let auth_b' = B.sub auth_b 0ul auth_len' in
B.blit plain_b plain_len' inout_b 0ul (uint64_to_uint32 plain_len % 16ul);
let h1 = get () in
B.blit auth_b auth_len' abytes_b 0ul (uint64_to_uint32 auth_len % 16ul);
let h1 = get () in
gcm256_encrypt_opt_alloca key iv plain_b' plain_len auth_b' auth_len iv_b out_b' tag_b keys_b
hkeys_b scratch_b inout_b abytes_b;
let h_post = get () in
B.blit inout_b
0ul
out_b
((uint64_to_uint32 plain_len / 16ul) * 16ul)
(uint64_to_uint32 plain_len % 16ul);
let h2 = get () in
assert (let plain_d = get_downview plain_b' in
length_aux3 plain_b' (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b' in
length_aux3 out_b' (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h1 plain_u) (UV.as_seq h1 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h_post out_u) (UV.as_seq h_post inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b' in
length_aux3 auth_b' (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h1 auth_u) (UV.as_seq h1 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\ (Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag =
gcm_encrypt_LE AES_256
(seq_nat32_to_seq_nat8_LE (Ghost.reveal key))
(Ghost.reveal iv)
plain_bytes
auth_bytes
in
Seq.equal cipher cipher_bytes /\ Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h_post tag_b)) tag
));
lemma_identical_uv out_b' h_post h2;
lemma_identical_uv inout_b h_post h2;
assert (let out_d = get_downview out_b' in
length_aux3 out_b' (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
UV.as_seq h_post out_u == UV.as_seq h2 out_u);
assert (let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
UV.as_seq h_post inout_u == UV.as_seq h2 inout_u);
lemma_slice_sub plain_b plain_b' inout_b h1;
lemma_slice_uv_extra plain_b plain_b' inout_b h1;
lemma_slice_sub auth_b auth_b' abytes_b h1;
lemma_slice_sub out_b out_b' inout_b h2;
lemma_slice_uv_extra auth_b auth_b' abytes_b h1;
lemma_slice_uv_extra out_b out_b' inout_b h2;
assert (let plain = seq_uint8_to_seq_nat8 (B.as_seq h1 plain_b) in
let auth = seq_uint8_to_seq_nat8 (B.as_seq h1 auth_b) in
let cipher, tag =
gcm_encrypt_LE AES_256
(seq_nat32_to_seq_nat8_LE (Ghost.reveal key))
(Ghost.reveal iv)
plain
auth
in
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h2 out_b)) cipher /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h2 tag_b)) tag);
pop_frame () | {
"checked_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst.checked",
"dependencies": [
"Vale.X64.MemoryAdapters.fsti.checked",
"Vale.X64.Decls.fsti.checked",
"Vale.Stdcalls.X64.GCMencryptOpt.fst.checked",
"Vale.SHA.Simplify_Sha.fsti.checked",
"Vale.Lib.BufferViewHelpers.fst.checked",
"Vale.Interop.Views.fsti.checked",
"Vale.AsLowStar.MemoryHelpers.fsti.checked",
"Vale.Arch.Types.fsti.checked",
"Vale.AES.Gcm_simplify.fsti.checked",
"Vale.AES.GCM_helpers.fsti.checked",
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Integers.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Vale.Wrapper.X64.GCMencryptOpt256.fst"
} | [
"total"
] | [
"FStar.Ghost.erased",
"FStar.Seq.Base.seq",
"Vale.Def.Types_s.nat32",
"Vale.AES.GCM_s.supported_iv_LE",
"Vale.Wrapper.X64.GCMencryptOpt.uint8_p",
"Vale.Wrapper.X64.GCMencryptOpt.uint64",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Prims._assert",
"Vale.Def.Types_s.nat8",
"Prims.l_and",
"FStar.Seq.Base.equal",
"Vale.Def.Words_s.nat8",
"Vale.Def.Words.Seq_s.seq_uint8_to_seq_nat8",
"LowStar.Monotonic.Buffer.as_seq",
"FStar.UInt8.t",
"LowStar.Buffer.trivial_preorder",
"FStar.Pervasives.Native.tuple2",
"Vale.AES.GCM_s.gcm_encrypt_LE",
"Vale.AES.AES_common_s.AES_256",
"Vale.Def.Words.Seq_s.seq_nat32_to_seq_nat8_LE",
"FStar.Ghost.reveal",
"Vale.Wrapper.X64.GCMencryptOpt256.lemma_slice_uv_extra",
"Vale.Wrapper.X64.GCMencryptOpt256.lemma_slice_sub",
"Prims.eq2",
"FStar.Seq.Properties.lseq",
"Vale.Def.Types_s.quad32",
"LowStar.BufferView.Up.length",
"LowStar.BufferView.Up.as_seq",
"LowStar.BufferView.Up.buffer",
"LowStar.BufferView.Up.mk_buffer",
"Vale.Interop.Views.up_view128",
"Vale.Wrapper.X64.GCMencryptOpt256.length_aux3",
"LowStar.BufferView.Down.buffer",
"Vale.Interop.Types.get_downview",
"Vale.Arch.HeapTypes_s.TUInt8",
"FStar.Integers.op_Slash",
"FStar.Integers.Signed",
"FStar.Integers.Winfinite",
"FStar.UInt64.v",
"Vale.Wrapper.X64.GCMencryptOpt256.lemma_identical_uv",
"Prims.b2t",
"FStar.Integers.op_Less",
"FStar.Seq.Base.length",
"Vale.Def.Words_s.pow2_32",
"Prims.logical",
"Vale.Wrapper.X64.GCMencryptOpt256.wrap_slice",
"Vale.Def.Types_s.le_seq_quad32_to_bytes",
"FStar.Seq.Base.append",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"LowStar.Monotonic.Buffer.blit",
"FStar.UInt32.__uint_to_t",
"FStar.Integers.op_Star",
"FStar.Integers.Unsigned",
"FStar.Integers.W32",
"FStar.Int.Cast.uint64_to_uint32",
"FStar.Integers.op_Percent",
"Vale.Wrapper.X64.GCMencryptOpt256.gcm256_encrypt_opt_alloca",
"LowStar.Monotonic.Buffer.mbuffer",
"LowStar.Buffer.sub",
"FStar.Ghost.hide",
"FStar.UInt32.t",
"FStar.Integers.int_t",
"Vale.Wrapper.X64.GCMencryptOpt256.math_cast_aux",
"FStar.HyperStack.ST.push_frame"
] | [] | module Vale.Wrapper.X64.GCMencryptOpt256
open FStar.Mul
open Vale.Stdcalls.X64.GCMencryptOpt
open Vale.AsLowStar.MemoryHelpers
open Vale.X64.MemoryAdapters
module V = Vale.X64.Decls
open Vale.SHA.Simplify_Sha
open Vale.AES.Gcm_simplify
open Vale.AES.GCM_helpers
open FStar.Calc
open FStar.Int.Cast
open FStar.Integers
open Vale.Arch.Types
open Vale.Lib.BufferViewHelpers
let wrap_slice (#a:Type0) (s:Seq.seq a) (i:int) : Seq.seq a =
Seq.slice s 0 (if 0 <= i && i <= Seq.length s then i else 0)
#set-options "--z3rlimit 400 --max_fuel 0 --max_ifuel 0"
let math_aux (n:nat) : Lemma (n * 1 == n) = ()
let length_div (b:uint8_p) : Lemma
(requires B.length b = 16)
(ensures DV.length (get_downview b) / 16 = 1)
= DV.length_eq (get_downview b)
inline_for_extraction
val gcm256_encrypt_opt':
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
auth_b:uint8_p ->
auth_bytes:uint64 ->
auth_num:uint64 ->
keys_b:uint8_p ->
iv_b:uint8_p ->
hkeys_b:uint8_p ->
abytes_b:uint8_p ->
in128x6_b:uint8_p ->
out128x6_b:uint8_p ->
len128x6:uint64 ->
in128_b:uint8_p ->
out128_b:uint8_p ->
len128_num:uint64 ->
inout_b:uint8_p ->
plain_num:uint64 ->
scratch_b:uint8_p ->
tag_b:uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint tag_b keys_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b abytes_b /\ B.disjoint tag_b iv_b /\
B.disjoint tag_b in128x6_b /\ B.disjoint tag_b out128x6_b /\
B.disjoint tag_b in128_b /\ B.disjoint tag_b out128_b /\
B.disjoint tag_b inout_b /\ B.disjoint tag_b scratch_b /\
B.disjoint tag_b hkeys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b auth_b /\
B.disjoint iv_b abytes_b /\ B.disjoint iv_b in128x6_b /\
B.disjoint iv_b out128x6_b /\ B.disjoint iv_b in128_b /\
B.disjoint iv_b out128_b /\ B.disjoint iv_b inout_b /\
B.disjoint iv_b scratch_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b auth_b /\
B.disjoint scratch_b abytes_b /\ B.disjoint scratch_b in128x6_b /\
B.disjoint scratch_b out128x6_b /\ B.disjoint scratch_b in128_b /\
B.disjoint scratch_b out128_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b hkeys_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b auth_b /\
B.disjoint inout_b abytes_b /\ B.disjoint inout_b in128x6_b /\
B.disjoint inout_b out128x6_b /\ B.disjoint inout_b in128_b /\
B.disjoint inout_b out128_b /\ B.disjoint inout_b hkeys_b /\
B.disjoint out128x6_b keys_b /\ B.disjoint out128x6_b auth_b /\
B.disjoint out128x6_b abytes_b /\ B.disjoint out128x6_b hkeys_b /\
B.disjoint out128x6_b in128_b /\ B.disjoint out128x6_b inout_b /\
B.disjoint in128x6_b keys_b /\ B.disjoint in128x6_b auth_b /\
B.disjoint in128x6_b abytes_b /\ B.disjoint in128x6_b hkeys_b /\
B.disjoint in128x6_b in128_b /\ B.disjoint in128x6_b inout_b /\
B.disjoint out128_b keys_b /\ B.disjoint out128_b auth_b /\
B.disjoint out128_b abytes_b /\ B.disjoint out128_b hkeys_b /\
B.disjoint out128_b in128x6_b /\ B.disjoint out128_b out128x6_b /\
B.disjoint out128_b inout_b /\
B.disjoint in128_b keys_b /\ B.disjoint in128_b auth_b /\
B.disjoint in128_b abytes_b /\ B.disjoint in128_b hkeys_b /\
B.disjoint in128_b in128x6_b /\ B.disjoint in128_b out128x6_b /\
B.disjoint in128_b inout_b /\
B.disjoint keys_b abytes_b /\ B.disjoint hkeys_b auth_b /\
B.disjoint hkeys_b abytes_b /\ B.disjoint auth_b abytes_b /\
B.disjoint keys_b auth_b /\
disjoint_or_eq in128x6_b out128x6_b /\
disjoint_or_eq in128_b out128_b /\
disjoint_or_eq keys_b hkeys_b /\
B.live h0 auth_b /\ B.live h0 abytes_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 in128x6_b /\ B.live h0 out128x6_b /\
B.live h0 in128_b /\ B.live h0 out128_b /\
B.live h0 inout_b /\ B.live h0 tag_b /\ B.live h0 scratch_b /\
B.length auth_b = 16 * UInt64.v auth_num /\
B.length abytes_b == 16 /\
B.length iv_b = 16 /\
B.length in128x6_b == 16 * UInt64.v len128x6 /\
B.length out128x6_b == B.length in128x6_b /\
B.length in128_b == 16 * UInt64.v len128_num /\
B.length out128_b == B.length in128_b /\
B.length inout_b == 16 /\
B.length scratch_b == 144 /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
UInt64.v plain_num < pow2_32 /\
UInt64.v auth_bytes < pow2_32 /\
UInt64.v len128x6 % 6 == 0 /\
(UInt64.v len128x6 > 0 ==> UInt64.v len128x6 >= 18) /\
12 + UInt64.v len128x6 + 6 < pow2_32 /\
UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) <= UInt64.v plain_num /\
UInt64.v plain_num < UInt64.v len128x6 * (128/8) + UInt64.v len128_num * (128/8) + 128/8 /\
UInt64.v auth_num * (128/8) <= UInt64.v auth_bytes /\
UInt64.v auth_bytes < UInt64.v auth_num * (128/8) + 128/8 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key))) /\
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
hkeys_reqs_pub (UV.as_seq h0 ub) (reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)))) /\
(length_div iv_b;
(low_buffer_read TUInt8 TUInt128 h0 iv_b 0) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out128x6_b)
(B.loc_union (B.loc_buffer out128_b)
(B.loc_buffer inout_b)))))) h0 h1 /\
((UInt64.v plain_num) < pow2_32 /\
(UInt64.v auth_bytes) < pow2_32 /\ (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6);
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num);
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6);
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num);
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let plain_in =
Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u)
in let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_num)
in let cipher_out =
Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u)
in let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_num)
in let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_num);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_bytes) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
is_aes_key AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
cipher == cipher_bytes /\
(length_div tag_b;
le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h1 tag_b 0) == tag
))))
)
#push-options "--smtencoding.nl_arith_repr boxwrap"
#set-options "--ext compat:normalizer_memo_ignore_cfg"
#restart-solver
inline_for_extraction
let gcm256_encrypt_opt' key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b =
let h0 = get() in
B.disjoint_neq iv_b auth_b;
B.disjoint_neq iv_b keys_b;
B.disjoint_neq iv_b hkeys_b;
B.disjoint_neq iv_b abytes_b;
B.disjoint_neq iv_b in128x6_b;
B.disjoint_neq iv_b out128x6_b;
B.disjoint_neq iv_b in128_b;
B.disjoint_neq iv_b out128_b;
B.disjoint_neq iv_b inout_b;
B.disjoint_neq iv_b scratch_b;
B.disjoint_neq iv_b tag_b;
DV.length_eq (get_downview auth_b);
DV.length_eq (get_downview keys_b);
DV.length_eq (get_downview iv_b);
DV.length_eq (get_downview hkeys_b);
DV.length_eq (get_downview abytes_b);
DV.length_eq (get_downview in128x6_b);
DV.length_eq (get_downview out128x6_b);
DV.length_eq (get_downview in128_b);
DV.length_eq (get_downview out128_b);
DV.length_eq (get_downview inout_b);
DV.length_eq (get_downview scratch_b);
DV.length_eq (get_downview tag_b);
math_aux (B.length auth_b);
math_aux (B.length keys_b);
math_aux (B.length iv_b);
math_aux (B.length hkeys_b);
math_aux (B.length in128x6_b);
math_aux (B.length scratch_b);
math_aux (B.length out128_b);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v auth_num) 16;
assert_norm (240 % 16 = 0);
assert_norm (16 % 16 = 0);
assert_norm (144 % 16 = 0);
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128x6) 16;
FStar.Math.Lemmas.cancel_mul_mod (UInt64.v len128_num) 16;
calc (<=) {
256 * ((16 * UInt64.v len128_num) / 16);
(==) { FStar.Math.Lemmas.cancel_mul_div (UInt64.v len128_num) 16 }
256 * (UInt64.v len128_num);
( <= ) { assert_norm (256 <= 4096); FStar.Math.Lemmas.lemma_mult_le_right (UInt64.v len128_num) 256 4096 }
4096 * (UInt64.v len128_num);
};
assert (DV.length (get_downview tag_b) % 16 = 0);
assert (DV.length (get_downview scratch_b) % 16 = 0);
assert (DV.length (get_downview out128_b) % 16 = 0);
as_vale_buffer_len #TUInt8 #TUInt128 auth_b;
as_vale_buffer_len #TUInt8 #TUInt128 keys_b;
as_vale_buffer_len #TUInt8 #TUInt128 iv_b;
as_vale_buffer_len #TUInt8 #TUInt128 hkeys_b;
as_vale_buffer_len #TUInt8 #TUInt128 abytes_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 out128x6_b;
as_vale_buffer_len #TUInt8 #TUInt128 in128_b;
as_vale_buffer_len #TUInt8 #TUInt128 inout_b;
as_vale_buffer_len #TUInt8 #TUInt128 scratch_b;
as_vale_buffer_len #TUInt8 #TUInt128 tag_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 auth_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128x6_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 in128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 out128_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 inout_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 iv_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 keys_b;
bounded_buffer_addrs_all TUInt8 TUInt128 h0 hkeys_b;
let (x, _) = gcm256_encrypt_opt key iv auth_b auth_bytes auth_num keys_b iv_b hkeys_b abytes_b
in128x6_b out128x6_b len128x6 in128_b out128_b len128_num inout_b plain_num scratch_b tag_b () in
let h1 = get() in
()
#pop-options
inline_for_extraction
val gcm256_encrypt_opt_alloca:
key:Ghost.erased (Seq.seq nat32) ->
iv:Ghost.erased supported_iv_LE ->
plain_b:uint8_p ->
plain_len:uint64 ->
auth_b:uint8_p ->
auth_len:uint64 ->
iv_b:uint8_p ->
out_b:uint8_p ->
tag_b:uint8_p ->
keys_b:uint8_p ->
hkeys_b:uint8_p ->
scratch_b:uint8_p ->
inout_b : uint8_p ->
abytes_b : uint8_p ->
Stack unit
(requires fun h0 ->
B.disjoint scratch_b tag_b /\ B.disjoint scratch_b out_b /\
B.disjoint scratch_b hkeys_b /\ B.disjoint scratch_b plain_b /\
B.disjoint scratch_b auth_b /\ B.disjoint scratch_b iv_b /\
B.disjoint scratch_b keys_b /\ B.disjoint scratch_b inout_b /\
B.disjoint scratch_b abytes_b /\
B.disjoint inout_b tag_b /\ B.disjoint inout_b out_b /\
B.disjoint inout_b hkeys_b /\ B.disjoint inout_b plain_b /\
B.disjoint inout_b auth_b /\ B.disjoint inout_b iv_b /\
B.disjoint inout_b keys_b /\ B.disjoint inout_b abytes_b /\
B.disjoint abytes_b tag_b /\ B.disjoint abytes_b out_b /\
B.disjoint abytes_b hkeys_b /\ B.disjoint abytes_b plain_b /\
B.disjoint abytes_b auth_b /\ B.disjoint abytes_b iv_b /\
B.disjoint abytes_b keys_b /\
B.disjoint tag_b out_b /\ B.disjoint tag_b hkeys_b /\
B.disjoint tag_b plain_b /\ B.disjoint tag_b auth_b /\
B.disjoint tag_b iv_b /\ disjoint_or_eq tag_b keys_b /\
B.disjoint iv_b keys_b /\ B.disjoint iv_b out_b /\
B.disjoint iv_b plain_b /\ B.disjoint iv_b hkeys_b /\
B.disjoint iv_b auth_b /\
B.disjoint out_b keys_b /\ B.disjoint out_b hkeys_b /\
B.disjoint out_b auth_b /\ disjoint_or_eq out_b plain_b /\
B.disjoint plain_b keys_b /\ B.disjoint plain_b hkeys_b /\
B.disjoint plain_b auth_b /\
disjoint_or_eq keys_b hkeys_b /\
B.disjoint keys_b auth_b /\ B.disjoint hkeys_b auth_b /\
B.live h0 auth_b /\ B.live h0 keys_b /\
B.live h0 iv_b /\ B.live h0 hkeys_b /\
B.live h0 out_b /\ B.live h0 plain_b /\
B.live h0 tag_b /\
B.live h0 scratch_b /\ B.live h0 inout_b /\ B.live h0 abytes_b /\
B.length auth_b = (UInt64.v auth_len / 16) * 16 /\
B.length iv_b = 16 /\
B.length plain_b = (UInt64.v plain_len / 16) * 16 /\
B.length out_b = B.length plain_b /\
B.length hkeys_b = 128 /\
B.length tag_b == 16 /\
B.length keys_b = 240 /\
B.length scratch_b = 144 /\
B.length inout_b = 16 /\
B.length abytes_b = 16 /\
UInt64.v plain_len < pow2_32 /\
UInt64.v auth_len < pow2_32 /\
aesni_enabled /\ pclmulqdq_enabled /\ avx_enabled /\ sse_enabled /\ movbe_enabled /\
is_aes_key_LE AES_256 (Ghost.reveal key) /\
(Seq.equal (B.as_seq h0 keys_b)
(seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key))))) /\
hkeys_reqs_pub (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
(reverse_bytes_quad32 (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))) /\
(le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b))) ==
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0)) (Ghost.reveal iv)
)
(ensures fun h0 _ h1 ->
B.modifies (B.loc_union (B.loc_buffer tag_b)
(B.loc_union (B.loc_buffer iv_b)
(B.loc_union (B.loc_buffer scratch_b)
(B.loc_union (B.loc_buffer out_b)
(B.loc_buffer inout_b))))) h0 h1 /\
(UInt64.v plain_len) < pow2_32 /\
(UInt64.v auth_len) < pow2_32 /\
(let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
let plain_in = Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u) in
let plain_bytes = wrap_slice (le_seq_quad32_to_bytes plain_in) (UInt64.v plain_len) in
let cipher_out = Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u) in
let cipher_bytes = wrap_slice (le_seq_quad32_to_bytes cipher_out) (UInt64.v plain_len) in
let auth_d = get_downview auth_b in
length_aux3 auth_b (UInt64.v auth_len / 16);
let auth_u = UV.mk_buffer auth_d Vale.Interop.Views.up_view128 in
let abytes_d = get_downview abytes_b in
length_aux3 abytes_b 1;
let abytes_u = UV.mk_buffer abytes_d Vale.Interop.Views.up_view128 in
let auth_in = Seq.append (UV.as_seq h0 auth_u) (UV.as_seq h0 abytes_u) in
let auth_bytes = wrap_slice (le_seq_quad32_to_bytes auth_in) (UInt64.v auth_len) in
(Seq.length plain_bytes) < pow2_32 /\
(Seq.length auth_bytes) < pow2_32 /\
(let cipher, tag = gcm_encrypt_LE AES_256 (seq_nat32_to_seq_nat8_LE (Ghost.reveal key)) (Ghost.reveal iv) plain_bytes auth_bytes in
Seq.equal cipher cipher_bytes /\
Seq.equal (seq_uint8_to_seq_nat8 (B.as_seq h1 tag_b)) tag
)
))
let lemma_same_seq_dv (h:HS.mem) (b:uint8_p) : Lemma
(Seq.equal (B.as_seq h b) (DV.as_seq h (get_downview b))) =
let db = get_downview b in
DV.length_eq db;
let aux (i:nat{i < B.length b}) : Lemma (Seq.index (B.as_seq h b) i == Seq.index (DV.as_seq h db) i) =
DV.as_seq_sel h db i;
DV.get_sel h db i;
Vale.Interop.Views.put8_reveal ()
in Classical.forall_intro aux
let lemma_uv_split (h:HS.mem) (b:uint8_p) (n:UInt32.t) : Lemma
(requires B.length b % 16 = 0 /\ UInt32.v n % 16 = 0 /\ UInt32.v n <= B.length b)
(ensures (
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
Seq.equal bs split_bs)
) =
let b1 = B.gsub b 0ul n in
let b2 = B.gsub b n (UInt32.uint_to_t (B.length b) - n) in
let b1_d = get_downview b1 in
length_aux3 b1 (B.length b1 / 16);
let b1_u = UV.mk_buffer b1_d Vale.Interop.Views.up_view128 in
let b2_d = get_downview b2 in
length_aux3 b2 (B.length b2 / 16);
let b2_u = UV.mk_buffer b2_d Vale.Interop.Views.up_view128 in
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
let split_bs = Seq.append (UV.as_seq h b1_u) (UV.as_seq h b2_u) in
let bs = UV.as_seq h b_u in
calc (==) {
Seq.length split_bs;
(==) { }
Seq.length (UV.as_seq h b1_u) + Seq.length (UV.as_seq h b2_u);
(==) { UV.length_eq b1_u; UV.length_eq b2_u }
DV.length b1_d / 16 + DV.length b2_d / 16;
(==) { DV.length_eq b1_d; DV.length_eq b2_d; math_aux (B.length b1); math_aux (B.length b2) }
B.length b1 / 16 + B.length b2 / 16;
(==) { }
B.length b / 16;
(==) { DV.length_eq b_d; UV.length_eq b_u; math_aux (B.length b) }
Seq.length bs;
};
assert (Seq.length bs == Seq.length split_bs);
let aux (i:nat{ i < Seq.length bs}) : Lemma (Seq.index bs i = Seq.index split_bs i)
=
UV.length_eq b_u;
lemma_same_seq_dv h b;
calc (==) {
Seq.index bs i;
(==) { UV.as_seq_sel h b_u i }
UV.sel h b_u i;
(==) { UV.get_sel h b_u i }
Vale.Interop.Views.get128 (Seq.slice (DV.as_seq h b_d) (i * 16) (i * 16 + 16));
(==) { lemma_same_seq_dv h b }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b) (i * 16) (i * 16 + 16));
(==) { assert (Seq.equal (B.as_seq h b) (Seq.append (B.as_seq h b1) (B.as_seq h b2))) }
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
};
if i < Seq.length (UV.as_seq h b1_u) then (
lemma_same_seq_dv h b1;
UV.length_eq b1_u;
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b1) (i * 16) (i * 16 + 16));
(==) { UV.get_sel h b1_u i }
UV.sel h b1_u i;
(==) { UV.as_seq_sel h b1_u i }
Seq.index (UV.as_seq h b1_u) i;
(==) { }
Seq.index split_bs i;
}
) else (
lemma_same_seq_dv h b2;
UV.length_eq b2_u;
let j = i - UV.length b1_u in
calc (==) {
Vale.Interop.Views.get128 (Seq.slice (Seq.append (B.as_seq h b1) (B.as_seq h b2)) (i * 16) (i * 16 + 16));
(==) { }
Vale.Interop.Views.get128 (Seq.slice (B.as_seq h b2) (j * 16) (j * 16 + 16));
(==) { UV.get_sel h b2_u j }
UV.sel h b2_u j;
(==) { UV.as_seq_sel h b2_u j }
Seq.index (UV.as_seq h b2_u) j;
(==) { }
Seq.index split_bs i;
}
)
in Classical.forall_intro aux
let math_cast_aux (n:UInt64.t) : Lemma
(requires UInt64.v n < pow2 32)
(ensures UInt32.v (uint64_to_uint32 n) = UInt64.v n)
= FStar.Math.Lemmas.small_mod (UInt64.v n) (pow2 32)
inline_for_extraction
let gcm256_encrypt_opt_alloca key iv plain_b plain_len auth_b auth_bytes iv_b
out_b tag_b keys_b hkeys_b scratch_b inout_b abytes_b =
let h0 = get() in
// Simplify the expression for the iv
DV.length_eq (get_downview iv_b);
length_aux4 iv_b;
calc (==) {
compute_iv_BE (aes_encrypt_LE AES_256 (Ghost.reveal key) (Mkfour 0 0 0 0))
(Ghost.reveal iv);
(==) { }
le_bytes_to_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 iv_b));
(==) { gcm_simplify2 iv_b h0 }
le_bytes_to_quad32 (le_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0));
(==) { le_bytes_to_quad32_to_bytes (low_buffer_read TUInt8 TUInt128 h0 iv_b 0) }
low_buffer_read TUInt8 TUInt128 h0 iv_b 0;
};
let lemma_uv_key () : Lemma
(let db = get_downview keys_b in
length_aux2 keys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 ub) (key_to_round_keys_LE AES_256 (Ghost.reveal key)))
= length_aux2 keys_b;
let db = get_downview keys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
le_bytes_to_seq_quad32_to_bytes (key_to_round_keys_LE AES_256 (Ghost.reveal key));
assert (Seq.equal (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b)))
(key_to_round_keys_LE AES_256 (Ghost.reveal key)));
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 keys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 keys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_key ();
// Simplify the precondition for hkeys_b
let lemma_uv_hkey () : Lemma
(let db = get_downview hkeys_b in
length_aux5 hkeys_b;
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
UV.as_seq h0 ub == le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b)))
= length_aux5 hkeys_b;
let db = get_downview hkeys_b in
let ub = UV.mk_buffer db Vale.Interop.Views.up_view128 in
calc (==) {
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h0 hkeys_b));
(==) { lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 hkeys_b h0 }
le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (seq_nat8_to_seq_uint8 (le_seq_quad32_to_bytes (UV.as_seq h0 ub))));
(==) { le_bytes_to_seq_quad32_to_bytes (UV.as_seq h0 ub) }
UV.as_seq h0 ub;
}
in lemma_uv_hkey ();
// Compute length of biggest blocks of 6 * 128-bit blocks
let len128x6 = UInt64.mul (plain_len / 96uL) 96uL in
if len128x6 / 16uL >= 18uL then (
let len128_num = ((plain_len / 16uL) * 16uL) - len128x6 in
// Casting to uint32 is here the equality
math_cast_aux len128x6;
math_cast_aux len128_num;
let in128x6_b = B.sub plain_b 0ul (uint64_to_uint32 len128x6) in
let out128x6_b = B.sub out_b 0ul (uint64_to_uint32 len128x6) in
let in128_b = B.sub plain_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b (uint64_to_uint32 len128x6) (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128x6' = UInt64.div len128x6 16uL in
let len128_num' = UInt64.div len128_num 16uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b (uint64_to_uint32 len128x6);
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b (uint64_to_uint32 len128x6);
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
) else (
let len128x6 = 0ul in
// Compute the size of the remaining 128-bit blocks
let len128_num = ((plain_len / 16uL) * 16uL) in
// Casting to uint32 is here the equality
FStar.Math.Lemmas.small_mod (UInt64.v len128_num) pow2_32;
let in128x6_b = B.sub plain_b 0ul len128x6 in
let out128x6_b = B.sub out_b 0ul len128x6 in
let in128_b = B.sub plain_b len128x6 (uint64_to_uint32 len128_num) in
let out128_b = B.sub out_b len128x6 (uint64_to_uint32 len128_num) in
let auth_num = UInt64.div auth_bytes 16uL in
let len128_num' = UInt64.div len128_num 16uL in
let len128x6' = 0uL in
gcm256_encrypt_opt'
key
iv
auth_b
auth_bytes
auth_num
keys_b
iv_b
hkeys_b
abytes_b
in128x6_b
out128x6_b
len128x6'
in128_b
out128_b
len128_num'
inout_b
plain_len
scratch_b
tag_b;
let h1 = get() in
lemma_uv_split h0 plain_b len128x6;
// Still need the two asserts for z3 to pick up seq equality
assert (
let in128x6_d = get_downview in128x6_b in
length_aux3 in128x6_b (UInt64.v len128x6');
let in128x6_u = UV.mk_buffer in128x6_d Vale.Interop.Views.up_view128 in
let in128_d = get_downview in128_b in
length_aux3 in128_b (UInt64.v len128_num');
let in128_u = UV.mk_buffer in128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let plain_d = get_downview plain_b in
length_aux3 plain_b (UInt64.v plain_len / 16);
let plain_u = UV.mk_buffer plain_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h0 in128x6_u) (UV.as_seq h0 in128_u))
(UV.as_seq h0 inout_u))
(Seq.append (UV.as_seq h0 plain_u) (UV.as_seq h0 inout_u)));
lemma_uv_split h1 out_b len128x6;
assert (
let out128x6_d = get_downview out128x6_b in
length_aux3 out128x6_b (UInt64.v len128x6');
let out128x6_u = UV.mk_buffer out128x6_d Vale.Interop.Views.up_view128 in
let out128_d = get_downview out128_b in
length_aux3 out128_b (UInt64.v len128_num');
let out128_u = UV.mk_buffer out128_d Vale.Interop.Views.up_view128 in
let inout_d = get_downview inout_b in
length_aux3 inout_b 1;
let inout_u = UV.mk_buffer inout_d Vale.Interop.Views.up_view128 in
let out_d = get_downview out_b in
length_aux3 out_b (UInt64.v plain_len / 16);
let out_u = UV.mk_buffer out_d Vale.Interop.Views.up_view128 in
Seq.equal
(Seq.append (Seq.append (UV.as_seq h1 out128x6_u) (UV.as_seq h1 out128_u))
(UV.as_seq h1 inout_u))
(Seq.append (UV.as_seq h1 out_u) (UV.as_seq h1 inout_u)))
);
// Simplify post condition for tag
let h_f = get() in
gcm_simplify2 tag_b h_f
let lemma_identical_uv (b:uint8_p) (h0 h1:HS.mem) : Lemma
(requires B.length b % 16 = 0 /\ Seq.equal (B.as_seq h0 b) (B.as_seq h1 b))
(ensures (
let b_d = get_downview b in
length_aux3 b (B.length b / 16);
let b_u = UV.mk_buffer b_d Vale.Interop.Views.up_view128 in
Seq.equal (UV.as_seq h0 b_u) (UV.as_seq h1 b_u)))
= lemma_dv_equal Vale.Interop.Views.down_view8 b h0 h1;
length_aux3 b (B.length b / 16);
lemma_uv_equal Vale.Interop.Views.up_view128 (get_downview b) h0 h1
let length_aux6 (b:uint8_p) : Lemma (B.length b = DV.length (get_downview b))
= DV.length_eq (get_downview b)
#push-options "--z3cliopt smt.arith.nl=true"
let lemma_slice_uv_extra (b:uint8_p) (b_start:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires
B.length b_start = B.length b / 16 * 16 /\
b_start == B.gsub b 0ul (UInt32.uint_to_t (B.length b_start)) /\
B.length b_extra = 16 /\
Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_start) (B.as_seq h b_extra)) 0 (B.length b))
)
(ensures (
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
Seq.equal sf (seq_uint8_to_seq_nat8 (B.as_seq h b))
))
=
let b_start_d = get_downview b_start in
length_aux6 b_start;
let b_start_u = UV.mk_buffer b_start_d Vale.Interop.Views.up_view128 in
let b_extra_d = get_downview b_extra in
length_aux6 b_extra;
let b_extra_u = UV.mk_buffer b_extra_d Vale.Interop.Views.up_view128 in
let suv = Seq.append (UV.as_seq h b_start_u) (UV.as_seq h b_extra_u) in
let sf = wrap_slice (le_seq_quad32_to_bytes suv) (B.length b) in
let b_f = seq_uint8_to_seq_nat8 (B.as_seq h b) in
// if B.length b > B.length b_start then (
calc (==) {
sf;
(==) { DV.length_eq b_start_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_start h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_start_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(UV.as_seq h b_extra_u)))
(B.length b);
(==) { DV.length_eq b_extra_d; lemma_seq_nat8_le_seq_quad32_to_bytes_uint32 b_extra h;
le_bytes_to_seq_quad32_to_bytes (UV.as_seq h b_extra_u) }
wrap_slice (le_seq_quad32_to_bytes (Seq.append
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { append_distributes_le_seq_quad32_to_bytes
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start)))
(le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
}
wrap_slice (Seq.append
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start))))
(le_seq_quad32_to_bytes (le_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))))
(B.length b);
(==) { le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_start));
le_seq_quad32_to_bytes_to_seq_quad32 (seq_uint8_to_seq_nat8 (B.as_seq h b_extra)) }
wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b);
(==) { Seq.lemma_eq_intro b_f
(wrap_slice (Seq.append
(seq_uint8_to_seq_nat8 (B.as_seq h b_start))
(seq_uint8_to_seq_nat8 (B.as_seq h b_extra)))
(B.length b))
}
b_f;
}
// ) else (
// calc (==) {
// sf;
// (==) { }
// wrap_slice (le_seq_quad32_to_bytes (UV.as_seq h b_start_u)) (B.length b);
// (==) { DV.length_eq (get_downview b_start);
// gcm_simplify1 b_start h (B.length b) }
// seq_uint8_to_seq_nat8 (B.as_seq h b_start);
// (==) { }
// b_f;
// }
// )
#pop-options
let lemma_slice_sub (b:uint8_p) (b_sub:uint8_p) (b_extra:uint8_p) (h:HS.mem) : Lemma
(requires B.length b_extra = 16 /\ B.length b_sub = B.length b / 16 * 16 /\
b_sub == B.gsub b 0ul (UInt32.uint_to_t (B.length b_sub)) /\
Seq.equal
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b_sub + B.length b % 16))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16))
)
(ensures Seq.equal
(B.as_seq h b)
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))
) =
calc (==) {
Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b);
(==) { Seq.lemma_eq_intro
(Seq.slice (Seq.append (B.as_seq h b_sub) (B.as_seq h b_extra)) 0 (B.length b))
(Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16)))
}
Seq.append (B.as_seq h b_sub) (Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
(==) { }
Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b_extra) 0 (B.length b % 16));
(==) { }
Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b));
(==) { Seq.lemma_eq_intro (B.as_seq h b)
(Seq.append
(Seq.slice (B.as_seq h b) 0 (B.length b_sub))
(Seq.slice (B.as_seq h b) (B.length b_sub) (B.length b)))
}
B.as_seq h b;
}
#set-options "--z3rlimit 600 --max_fuel 0 --max_ifuel 0" | false | true | Vale.Wrapper.X64.GCMencryptOpt256.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"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": 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": 600,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gcm256_encrypt_opt_stdcall: Vale.Wrapper.X64.GCMencryptOpt.encrypt_opt_stdcall_st AES_256 | [] | Vale.Wrapper.X64.GCMencryptOpt256.gcm256_encrypt_opt_stdcall | {
"file_name": "vale/code/arch/x64/interop/Vale.Wrapper.X64.GCMencryptOpt256.fst",
"git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e",
"git_url": "https://github.com/hacl-star/hacl-star.git",
"project_name": "hacl-star"
} | Vale.Wrapper.X64.GCMencryptOpt.encrypt_opt_stdcall_st Vale.AES.AES_common_s.AES_256 | {
"end_col": 13,
"end_line": 1003,
"start_col": 115,
"start_line": 883
} |
Prims.Tot | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let t = input_buffer | let t = | false | null | false | input_buffer | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [
"total"
] | [
"EverParse3d.InputStream.Buffer.Aux.input_buffer"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction | false | true | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val t : Type0 | [] | EverParse3d.InputStream.Buffer.Aux.t | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | Type0 | {
"end_col": 20,
"end_line": 35,
"start_col": 8,
"start_line": 35
} |
|
FStar.HyperStack.ST.Stack | val default_error_handler
(typename_s fieldname reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h')) | [
{
"abbrev": false,
"full_module": "LowStar.BufferOps",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let default_error_handler
(typename_s: string)
(fieldname: string)
(reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h'))
=
if not ( !* context ).EverParse3d.ErrorCode.filled then begin
context *= {
EverParse3d.ErrorCode.filled = true;
EverParse3d.ErrorCode.start_pos = start_pos;
EverParse3d.ErrorCode.typename_s = typename_s;
EverParse3d.ErrorCode.fieldname = fieldname;
EverParse3d.ErrorCode.reason = reason;
EverParse3d.ErrorCode.error_code = error_code;
}
end | val default_error_handler
(typename_s fieldname reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h'))
let default_error_handler
(typename_s fieldname reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h')) = | true | null | false | if not (!*context).EverParse3d.ErrorCode.filled
then
context *=
{
EverParse3d.ErrorCode.filled = true;
EverParse3d.ErrorCode.start_pos = start_pos;
EverParse3d.ErrorCode.typename_s = typename_s;
EverParse3d.ErrorCode.fieldname = fieldname;
EverParse3d.ErrorCode.reason = reason;
EverParse3d.ErrorCode.error_code = error_code
} | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [] | [
"Prims.string",
"FStar.UInt64.t",
"LowStar.Buffer.pointer",
"EverParse3d.ErrorCode.error_frame",
"EverParse3d.InputStream.Buffer.Aux.input_buffer",
"LowStar.BufferOps.op_Star_Equals",
"LowStar.Buffer.trivial_preorder",
"EverParse3d.ErrorCode.Mkerror_frame",
"Prims.unit",
"Prims.bool",
"Prims.op_Negation",
"EverParse3d.ErrorCode.__proj__Mkerror_frame__item__filled",
"LowStar.BufferOps.op_Bang_Star",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Monotonic.Buffer.live",
"LowStar.Monotonic.Buffer.modifies",
"LowStar.Monotonic.Buffer.loc_buffer"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction
noextract
let t = input_buffer
unfold
let _live
(x: t)
(h: HS.mem)
: Tot prop
=
IB.live_input_buffer h x.buf x.perm_of /\
B.live h x.pos /\
U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\
IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all
let _footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y))
= B.loc_buffer (IB.slice_of x.buf).LP.base `B.loc_union` IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos
let _perm_footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> _footprint x `B.loc_includes` y))
= IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos
let _get_remaining
(x: t)
(h: HS.mem)
: Ghost (Seq.seq U8.t)
(requires (_live x h))
(ensures (fun y -> Seq.length y <= U32.v x.len))
=
let i = U32.v (B.deref h x.pos) in
Seq.slice x.g_all i (Seq.length x.g_all)
(* default error handler *)
open LowStar.BufferOps
let default_error_handler
(typename_s: string)
(fieldname: string)
(reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h')) | false | false | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val default_error_handler
(typename_s fieldname reason: string)
(error_code: U64.t)
(context: B.pointer EverParse3d.ErrorCode.error_frame)
(input: input_buffer)
(start_pos: U64.t)
: HST.Stack unit
(requires (fun h -> B.live h context))
(ensures (fun h _ h' -> B.modifies (B.loc_buffer context) h h')) | [] | EverParse3d.InputStream.Buffer.Aux.default_error_handler | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
typename_s: Prims.string ->
fieldname: Prims.string ->
reason: Prims.string ->
error_code: FStar.UInt64.t ->
context: LowStar.Buffer.pointer EverParse3d.ErrorCode.error_frame ->
input: EverParse3d.InputStream.Buffer.Aux.input_buffer ->
start_pos: FStar.UInt64.t
-> FStar.HyperStack.ST.Stack Prims.unit | {
"end_col": 5,
"end_line": 100,
"start_col": 2,
"start_line": 91
} |
Prims.Ghost | val _perm_footprint (x: t)
: Ghost B.loc (requires True) (ensures (fun y -> (_footprint x) `B.loc_includes` y)) | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let _perm_footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> _footprint x `B.loc_includes` y))
= IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos | val _perm_footprint (x: t)
: Ghost B.loc (requires True) (ensures (fun y -> (_footprint x) `B.loc_includes` y))
let _perm_footprint (x: t)
: Ghost B.loc (requires True) (ensures (fun y -> (_footprint x) `B.loc_includes` y)) = | false | null | false | (IR.loc_perm x.perm_of) `B.loc_union` (B.loc_buffer x.pos) | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [] | [
"EverParse3d.InputStream.Buffer.Aux.t",
"LowStar.Monotonic.Buffer.loc_union",
"EverParse3d.Readable.loc_perm",
"LowParse.Bytes.byte",
"LowParse.Slice.__proj__Mkslice__item__base",
"EverParse3d.InputBuffer.triv",
"EverParse3d.InputBuffer.slice_of",
"FStar.Ghost.reveal",
"FStar.UInt32.t",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__len0",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__buf",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__perm_of",
"LowStar.Monotonic.Buffer.loc_buffer",
"FStar.Ghost.erased",
"LowStar.Buffer.trivial_preorder",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__pos",
"LowStar.Monotonic.Buffer.loc",
"Prims.l_True",
"LowStar.Monotonic.Buffer.loc_includes",
"EverParse3d.InputStream.Buffer.Aux._footprint"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction
noextract
let t = input_buffer
unfold
let _live
(x: t)
(h: HS.mem)
: Tot prop
=
IB.live_input_buffer h x.buf x.perm_of /\
B.live h x.pos /\
U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\
IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all
let _footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y))
= B.loc_buffer (IB.slice_of x.buf).LP.base `B.loc_union` IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos
let _perm_footprint
(x: t)
: Ghost B.loc
(requires True) | false | false | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val _perm_footprint (x: t)
: Ghost B.loc (requires True) (ensures (fun y -> (_footprint x) `B.loc_includes` y)) | [] | EverParse3d.InputStream.Buffer.Aux._perm_footprint | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: EverParse3d.InputStream.Buffer.Aux.t -> Prims.Ghost LowStar.Monotonic.Buffer.loc | {
"end_col": 56,
"end_line": 63,
"start_col": 2,
"start_line": 63
} |
Prims.Ghost | val _footprint (x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y)) | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let _footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y))
= B.loc_buffer (IB.slice_of x.buf).LP.base `B.loc_union` IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos | val _footprint (x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y))
let _footprint (x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y)) = | false | null | false | ((B.loc_buffer (IB.slice_of x.buf).LP.base) `B.loc_union` (IR.loc_perm x.perm_of))
`B.loc_union`
(B.loc_buffer x.pos) | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [] | [
"EverParse3d.InputStream.Buffer.Aux.t",
"LowStar.Monotonic.Buffer.loc_union",
"LowStar.Monotonic.Buffer.loc_buffer",
"LowParse.Bytes.byte",
"LowParse.Slice.buffer_srel_of_srel",
"EverParse3d.InputBuffer.triv",
"LowParse.Slice.__proj__Mkslice__item__base",
"EverParse3d.InputBuffer.slice_of",
"FStar.Ghost.reveal",
"FStar.UInt32.t",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__len0",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__buf",
"EverParse3d.Readable.loc_perm",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__perm_of",
"FStar.Ghost.erased",
"LowStar.Buffer.trivial_preorder",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__pos",
"LowStar.Monotonic.Buffer.loc",
"Prims.l_True",
"LowStar.Monotonic.Buffer.loc_includes",
"LowStar.Monotonic.Buffer.address_liveness_insensitive_locs"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction
noextract
let t = input_buffer
unfold
let _live
(x: t)
(h: HS.mem)
: Tot prop
=
IB.live_input_buffer h x.buf x.perm_of /\
B.live h x.pos /\
U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\
IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all
let _footprint
(x: t)
: Ghost B.loc
(requires True) | false | false | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val _footprint (x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y)) | [] | EverParse3d.InputStream.Buffer.Aux._footprint | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: EverParse3d.InputStream.Buffer.Aux.t -> Prims.Ghost LowStar.Monotonic.Buffer.loc | {
"end_col": 111,
"end_line": 56,
"start_col": 2,
"start_line": 56
} |
Prims.Tot | val _live (x: t) (h: HS.mem) : Tot prop | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let _live
(x: t)
(h: HS.mem)
: Tot prop
=
IB.live_input_buffer h x.buf x.perm_of /\
B.live h x.pos /\
U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\
IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all | val _live (x: t) (h: HS.mem) : Tot prop
let _live (x: t) (h: HS.mem) : Tot prop = | false | null | false | IB.live_input_buffer h x.buf x.perm_of /\ B.live h x.pos /\ U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\ IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [
"total"
] | [
"EverParse3d.InputStream.Buffer.Aux.t",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_and",
"EverParse3d.InputBuffer.live_input_buffer",
"FStar.Ghost.reveal",
"FStar.UInt32.t",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__len0",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__buf",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__perm_of",
"LowStar.Monotonic.Buffer.live",
"FStar.Ghost.erased",
"LowStar.Buffer.trivial_preorder",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__pos",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"LowStar.Monotonic.Buffer.deref",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__len",
"Prims.eq2",
"FStar.Seq.Base.seq",
"LowParse.Bytes.byte",
"LowStar.Monotonic.Buffer.as_seq",
"LowParse.Slice.buffer_srel_of_srel",
"EverParse3d.InputBuffer.triv",
"LowParse.Slice.__proj__Mkslice__item__base",
"EverParse3d.InputBuffer.slice_of",
"FStar.UInt8.t",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__g_all_buf",
"EverParse3d.Readable.unreadable",
"FStar.UInt32.__uint_to_t",
"EverParse3d.Readable.readable",
"FStar.Seq.Base.slice",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__g_all",
"Prims.prop"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction
noextract
let t = input_buffer
unfold
let _live
(x: t)
(h: HS.mem)
: Tot prop | false | true | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val _live (x: t) (h: HS.mem) : Tot prop | [] | EverParse3d.InputStream.Buffer.Aux._live | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: EverParse3d.InputStream.Buffer.Aux.t -> h: FStar.Monotonic.HyperStack.mem -> Prims.prop | {
"end_col": 92,
"end_line": 49,
"start_col": 2,
"start_line": 43
} |
Prims.Ghost | val _get_remaining (x: t) (h: HS.mem)
: Ghost (Seq.seq U8.t) (requires (_live x h)) (ensures (fun y -> Seq.length y <= U32.v x.len)) | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"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": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "LowParse.Low.Base",
"short_module": "LP"
},
{
"abbrev": true,
"full_module": "EverParse3d.Readable",
"short_module": "IR"
},
{
"abbrev": true,
"full_module": "EverParse3d.InputBuffer",
"short_module": "IB"
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "EverParse3d.InputStream.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let _get_remaining
(x: t)
(h: HS.mem)
: Ghost (Seq.seq U8.t)
(requires (_live x h))
(ensures (fun y -> Seq.length y <= U32.v x.len))
=
let i = U32.v (B.deref h x.pos) in
Seq.slice x.g_all i (Seq.length x.g_all) | val _get_remaining (x: t) (h: HS.mem)
: Ghost (Seq.seq U8.t) (requires (_live x h)) (ensures (fun y -> Seq.length y <= U32.v x.len))
let _get_remaining (x: t) (h: HS.mem)
: Ghost (Seq.seq U8.t) (requires (_live x h)) (ensures (fun y -> Seq.length y <= U32.v x.len)) = | false | null | false | let i = U32.v (B.deref h x.pos) in
Seq.slice x.g_all i (Seq.length x.g_all) | {
"checked_file": "EverParse3d.InputStream.Buffer.Aux.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.BufferOps.fst.checked",
"LowStar.Buffer.fst.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked",
"EverParse3d.Readable.fsti.checked",
"EverParse3d.InputBuffer.fsti.checked",
"EverParse3d.ErrorCode.fst.checked"
],
"interface_file": false,
"source_file": "EverParse3d.InputStream.Buffer.Aux.fst"
} | [] | [
"EverParse3d.InputStream.Buffer.Aux.t",
"FStar.Monotonic.HyperStack.mem",
"FStar.Seq.Base.slice",
"FStar.UInt8.t",
"FStar.Ghost.reveal",
"FStar.Seq.Base.seq",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__g_all",
"FStar.Seq.Base.length",
"FStar.UInt.uint_t",
"FStar.UInt32.v",
"FStar.UInt32.t",
"LowStar.Monotonic.Buffer.deref",
"FStar.Ghost.erased",
"LowStar.Buffer.trivial_preorder",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__pos",
"EverParse3d.InputStream.Buffer.Aux._live",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"EverParse3d.InputStream.Buffer.Aux.__proj__Mkinput_buffer__item__len"
] | [] | module EverParse3d.InputStream.Buffer.Aux
(* This module is here to break circularity in KaRaMeL bundles (because Prims must be in the EverParse bundle because of the string type, as opposed to C,FStar,LowStar.) *)
module IB = EverParse3d.InputBuffer
module IR = EverParse3d.Readable
module LP = LowParse.Low.Base
module B = LowStar.Buffer
module U8 = FStar.UInt8
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
noeq
type input_buffer = {
len0: Ghost.erased U32.t;
buf: IB.input_buffer_t len0;
perm_of: IR.perm (IB.slice_of buf).LP.base;
len: Ghost.erased U32.t;
pos: B.pointer (Ghost.erased U32.t);
g_all_buf: Ghost.erased (Seq.seq U8.t);
g_all: Ghost.erased (Seq.seq U8.t);
prf: squash (
U32.v len <= U32.v len0 /\
Seq.length g_all == U32.v len /\
B.loc_disjoint (B.loc_buffer (IB.slice_of buf).LP.base `B.loc_union` IR.loc_perm perm_of) (B.loc_buffer pos) /\
Seq.length g_all_buf == U32.v len0 /\
Ghost.reveal g_all == Seq.slice g_all_buf 0 (U32.v len)
);
}
inline_for_extraction
noextract
let t = input_buffer
unfold
let _live
(x: t)
(h: HS.mem)
: Tot prop
=
IB.live_input_buffer h x.buf x.perm_of /\
B.live h x.pos /\
U32.v (B.deref h x.pos) <= U32.v x.len /\
B.as_seq h (IB.slice_of x.buf).LP.base == Ghost.reveal x.g_all_buf /\
IR.unreadable h x.perm_of 0ul (B.deref h x.pos) /\
IR.readable h x.perm_of (B.deref h x.pos) x.len0 /\
Seq.slice (B.as_seq h (IB.slice_of x.buf).LP.base) 0 (U32.v x.len) == Ghost.reveal x.g_all
let _footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> B.address_liveness_insensitive_locs `B.loc_includes` y))
= B.loc_buffer (IB.slice_of x.buf).LP.base `B.loc_union` IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos
let _perm_footprint
(x: t)
: Ghost B.loc
(requires True)
(ensures (fun y -> _footprint x `B.loc_includes` y))
= IR.loc_perm x.perm_of `B.loc_union` B.loc_buffer x.pos
let _get_remaining
(x: t)
(h: HS.mem)
: Ghost (Seq.seq U8.t)
(requires (_live x h)) | false | false | EverParse3d.InputStream.Buffer.Aux.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 2,
"max_fuel": 0,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "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.qi.eager_threshold=100"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 8,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val _get_remaining (x: t) (h: HS.mem)
: Ghost (Seq.seq U8.t) (requires (_live x h)) (ensures (fun y -> Seq.length y <= U32.v x.len)) | [] | EverParse3d.InputStream.Buffer.Aux._get_remaining | {
"file_name": "src/3d/prelude/buffer/EverParse3d.InputStream.Buffer.Aux.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: EverParse3d.InputStream.Buffer.Aux.t -> h: FStar.Monotonic.HyperStack.mem
-> Prims.Ghost (FStar.Seq.Base.seq FStar.UInt8.t) | {
"end_col": 42,
"end_line": 73,
"start_col": 1,
"start_line": 71
} |
Prims.Tot | val _zero_for_deref:FStar.UInt32.t | [
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let _zero_for_deref : FStar.UInt32.t = 0ul | val _zero_for_deref:FStar.UInt32.t
let _zero_for_deref:FStar.UInt32.t = | false | null | false | 0ul | {
"checked_file": "C.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Int32.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "C.fst"
} | [
"total"
] | [
"FStar.UInt32.__uint_to_t"
] | [] | module C
open FStar.HyperStack.ST
module HS = FStar.HyperStack
module U8 = FStar.UInt8
// This module contains a series of bindings that already exist in C. It receives
// a special treatment in Karamel (no prefixes, no .c/.h generated).
// - If a value already exists (e.g. char or srand), then it is defined via the
// default #includes.
// - If a value doesn't exist, it is defined in krmllib.h and implemented in
// krmllib.c (e.g. exit_success, instead of EXIT_SUCCESS).
// The C standard library
assume val srand: UInt32.t -> Stack unit
(requires (fun _ -> true))
(ensures (fun h0 _ h1 -> h0 == h1))
assume val rand: unit -> Stack Int32.t
(requires (fun _ -> true))
(ensures (fun h0 _ h1 -> h0 == h1))
assume val exit: Int32.t -> Stack unit
(requires (fun _ -> True))
(ensures (fun h0 _ h1 -> False))
// Re-routed to KRML_HOST_EXIT for environments which don't have a libc
assume val portable_exit: Int32.t -> Stack unit
(requires (fun _ -> True))
(ensures (fun h0 _ h1 -> False))
assume type channel
assume val stdout: channel
assume val stderr: channel
assume val fflush: channel -> St Int32.t
// Abstract char type, with explicit conversions to/from uint8
assume val char: Type0
assume HasEq_char: hasEq char
assume val char_of_uint8: U8.t -> char
assume val uint8_of_char: char -> U8.t
assume val char_uint8 (c: char): Lemma (ensures (char_of_uint8 (uint8_of_char c) = c))
[ SMTPat (uint8_of_char c) ]
assume val uint8_char (u: U8.t): Lemma (ensures (uint8_of_char (char_of_uint8 u) = u))
[ SMTPat (char_of_uint8 u) ]
// Clocks
assume val int: Type0
assume val clock_t: Type0
assume val clock: unit -> Stack clock_t
(requires (fun _ -> true))
(ensures (fun h0 _ h1 -> modifies_none h0 h1))
// C stdlib; the order of these constructors matters for Wasm. When emitting C
// code, this type gets a special case and is not emitted to C.
type exit_code = | EXIT_SUCCESS | EXIT_FAILURE
// Debugging
assume val print_bytes: b:LowStar.Buffer.buffer UInt8.t -> len:UInt32.t{UInt32.v len <= LowStar.Buffer.length b} -> Stack unit
(requires (fun h -> LowStar.Buffer.live h b))
(ensures (fun h0 _ h1 -> h0 == h1))
// An index to be used as argument to Buffer.index so that
// b[_zero_for_deref] is turned into *b
// Special treatment: marked to not be emitted to C | false | true | C.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val _zero_for_deref:FStar.UInt32.t | [] | C._zero_for_deref | {
"file_name": "krmllib/C.fst",
"git_rev": "a7be2a7c43eca637ceb57fe8f3ffd16fc6627ebd",
"git_url": "https://github.com/FStarLang/karamel.git",
"project_name": "karamel"
} | FStar.UInt32.t | {
"end_col": 42,
"end_line": 65,
"start_col": 39,
"start_line": 65
} |
Prims.Tot | val rg_dummy (#a #rst: _) (rg: regional rst a) : Tot a | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rg_dummy #a #rst (rg:regional rst a)
: Tot a
= Rgl?.dummy rg | val rg_dummy (#a #rst: _) (rg: regional rst a) : Tot a
let rg_dummy #a #rst (rg: regional rst a) : Tot a = | false | null | false | Rgl?.dummy rg | {
"checked_file": "LowStar.Regional.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "LowStar.Regional.fst"
} | [
"total"
] | [
"LowStar.Regional.regional",
"LowStar.Regional.__proj__Rgl__item__dummy"
] | [] | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module LowStar.Regional
(**
* This module defines what is conceptually a typeclass called
* `regional` (although it is not syntactically marked as a `class`
* yet).
*
* `regional a` is the the class of types whose values have explicit
* memory allocations confined spatially within a single heap region
* in the hyperstack memory model.
*
* Being confined to a region, values of regional types support a
* natural framing principles: state mutations that do not overlap
* with a regional value's region are noninterfering.
*
* Instances of regional types are given for buffers and vectors:
* See LowStar.Regional.Instances, LowStar.RVector for samples.
*
*)
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
/// Regionality
/// Motivation: we want to ensure that all stateful operations for a value of
/// type `a` are within the `region_of` the value.
///
/// Furthermore, we would like regional to be parameterized over another type class
/// that elements can have. However, we are also trying to extract to C, meaning
/// that we can't incur any run-time lookups and indirections. In essence, we'd
/// like for members of a regional to potentially be partial applications where
/// the first argument may be an agility parameter, an extra type class for the
/// elements, etc. etc. except that partial applications are not allowed in C.
///
/// We therefore add an "st" type, which is a piece of (pure) state (hence more
/// like a parameter) that functions are allowed to capture. Currently, only
/// ``r_alloc`` needs that extra parameter. The parameter is stored within the
/// type class, so that clients are not required to manage that piece of state
/// themselves. This is, in effect, closure-conversion for ``r_alloc`` where the
/// closure state is lifted and stored in the regional itself. As such, the only
/// piece of state that ``r_alloc`` may receive is the exact value that was
/// captured.
///
/// Several alternative designs exist, e.g. making ``a`` at type ``st -> Type0``
/// (won't extract); instantiating ``st`` as a singleton type and dropping the
/// refinement (also works, but doesn't make the intent of closure-conversion
/// explicit); dropping the refinement and leaving it up to the user to store
/// the refinement in ``r_inv`` (which would then take ``state`` as an
/// argument)...
noeq type regional (st:Type) (a:Type0) =
| Rgl:
// This is not really a piece of state, but more like a parameter.
state: st ->
// The target type should have a region where it belongs.
region_of: (a -> GTot HS.rid) ->
//loc_of for the underlying a
loc_of: (a -> GTot loc) ->
// A parameterless value of type `a`.
// It does not have to satisfy the invariant `r_inv` described below.
dummy: a ->
// An invariant we want to maintain for each operation.
// For example, it may include `live` and `freeable` properties
// for related objects.
r_inv: (HS.mem -> a -> GTot Type0) ->
r_inv_reg:
(h:HS.mem -> v:a ->
Lemma (requires (r_inv h v))
(ensures (HS.live_region h (region_of v)))) ->
// A representation type of `a` and a corresponding conversion function
repr: Type0 ->
r_repr: (h:HS.mem -> v:a{r_inv h v} -> GTot repr) ->
// A core separation lemma, saying that the invariant and representation are
// preserved when an orthogonal state transition happens.
r_sep:
(v:a -> p:loc -> h:HS.mem -> h':HS.mem ->
Lemma (requires (r_inv h v /\
loc_disjoint (loc_all_regions_from false (region_of v)) p /\
modifies p h h'))
(ensures (r_inv h' v /\ r_repr h v == r_repr h' v))) ->
/// Allocation
// The representation for the initial value of type `a`
irepr: Ghost.erased repr ->
// A property that should hold for all initial values of type `a`.
r_alloc_p: (a -> GTot Type0) ->
// An allocation operation. We might have several ways of initializing a
// given target type `a`; then multiple typeclass instances should be
// defined, and each of them can be used properly.
r_alloc: ((s:st { s == state }) -> r:HST.erid ->
HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (loc_of v) h0 h1 /\ //the underlying loc is fresh
r_alloc_p v /\ r_inv h1 v /\ region_of v == r /\
r_repr h1 v == Ghost.reveal irepr))) ->
// Destruction: note that it allows to `modify` all the regions, including
// its subregions. It is fair when we want to `free` a vector and its
// elements as well, assuming the elements belong to subregions.
r_free: ((s:st { s == state }) -> v:a ->
HST.ST unit
(requires (fun h0 -> r_inv h0 v))
(ensures (fun h0 _ h1 ->
modifies (loc_all_regions_from false (region_of v)) h0 h1))) ->
regional st a
let rg_inv #a #rst (rg: regional rst a) =
Rgl?.r_inv rg
inline_for_extraction
let rg_dummy #a #rst (rg:regional rst a) | false | false | LowStar.Regional.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val rg_dummy (#a #rst: _) (rg: regional rst a) : Tot a | [] | LowStar.Regional.rg_dummy | {
"file_name": "ulib/LowStar.Regional.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | rg: LowStar.Regional.regional rst a -> a | {
"end_col": 15,
"end_line": 144,
"start_col": 2,
"start_line": 144
} |
Prims.GTot | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rg_inv #a #rst (rg: regional rst a) =
Rgl?.r_inv rg | let rg_inv #a #rst (rg: regional rst a) = | false | null | false | Rgl?.r_inv rg | {
"checked_file": "LowStar.Regional.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "LowStar.Regional.fst"
} | [
"sometrivial"
] | [
"LowStar.Regional.regional",
"LowStar.Regional.__proj__Rgl__item__r_inv",
"FStar.Monotonic.HyperStack.mem"
] | [] | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module LowStar.Regional
(**
* This module defines what is conceptually a typeclass called
* `regional` (although it is not syntactically marked as a `class`
* yet).
*
* `regional a` is the the class of types whose values have explicit
* memory allocations confined spatially within a single heap region
* in the hyperstack memory model.
*
* Being confined to a region, values of regional types support a
* natural framing principles: state mutations that do not overlap
* with a regional value's region are noninterfering.
*
* Instances of regional types are given for buffers and vectors:
* See LowStar.Regional.Instances, LowStar.RVector for samples.
*
*)
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
/// Regionality
/// Motivation: we want to ensure that all stateful operations for a value of
/// type `a` are within the `region_of` the value.
///
/// Furthermore, we would like regional to be parameterized over another type class
/// that elements can have. However, we are also trying to extract to C, meaning
/// that we can't incur any run-time lookups and indirections. In essence, we'd
/// like for members of a regional to potentially be partial applications where
/// the first argument may be an agility parameter, an extra type class for the
/// elements, etc. etc. except that partial applications are not allowed in C.
///
/// We therefore add an "st" type, which is a piece of (pure) state (hence more
/// like a parameter) that functions are allowed to capture. Currently, only
/// ``r_alloc`` needs that extra parameter. The parameter is stored within the
/// type class, so that clients are not required to manage that piece of state
/// themselves. This is, in effect, closure-conversion for ``r_alloc`` where the
/// closure state is lifted and stored in the regional itself. As such, the only
/// piece of state that ``r_alloc`` may receive is the exact value that was
/// captured.
///
/// Several alternative designs exist, e.g. making ``a`` at type ``st -> Type0``
/// (won't extract); instantiating ``st`` as a singleton type and dropping the
/// refinement (also works, but doesn't make the intent of closure-conversion
/// explicit); dropping the refinement and leaving it up to the user to store
/// the refinement in ``r_inv`` (which would then take ``state`` as an
/// argument)...
noeq type regional (st:Type) (a:Type0) =
| Rgl:
// This is not really a piece of state, but more like a parameter.
state: st ->
// The target type should have a region where it belongs.
region_of: (a -> GTot HS.rid) ->
//loc_of for the underlying a
loc_of: (a -> GTot loc) ->
// A parameterless value of type `a`.
// It does not have to satisfy the invariant `r_inv` described below.
dummy: a ->
// An invariant we want to maintain for each operation.
// For example, it may include `live` and `freeable` properties
// for related objects.
r_inv: (HS.mem -> a -> GTot Type0) ->
r_inv_reg:
(h:HS.mem -> v:a ->
Lemma (requires (r_inv h v))
(ensures (HS.live_region h (region_of v)))) ->
// A representation type of `a` and a corresponding conversion function
repr: Type0 ->
r_repr: (h:HS.mem -> v:a{r_inv h v} -> GTot repr) ->
// A core separation lemma, saying that the invariant and representation are
// preserved when an orthogonal state transition happens.
r_sep:
(v:a -> p:loc -> h:HS.mem -> h':HS.mem ->
Lemma (requires (r_inv h v /\
loc_disjoint (loc_all_regions_from false (region_of v)) p /\
modifies p h h'))
(ensures (r_inv h' v /\ r_repr h v == r_repr h' v))) ->
/// Allocation
// The representation for the initial value of type `a`
irepr: Ghost.erased repr ->
// A property that should hold for all initial values of type `a`.
r_alloc_p: (a -> GTot Type0) ->
// An allocation operation. We might have several ways of initializing a
// given target type `a`; then multiple typeclass instances should be
// defined, and each of them can be used properly.
r_alloc: ((s:st { s == state }) -> r:HST.erid ->
HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (loc_of v) h0 h1 /\ //the underlying loc is fresh
r_alloc_p v /\ r_inv h1 v /\ region_of v == r /\
r_repr h1 v == Ghost.reveal irepr))) ->
// Destruction: note that it allows to `modify` all the regions, including
// its subregions. It is fair when we want to `free` a vector and its
// elements as well, assuming the elements belong to subregions.
r_free: ((s:st { s == state }) -> v:a ->
HST.ST unit
(requires (fun h0 -> r_inv h0 v))
(ensures (fun h0 _ h1 ->
modifies (loc_all_regions_from false (region_of v)) h0 h1))) ->
regional st a | false | false | LowStar.Regional.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val rg_inv : rg: LowStar.Regional.regional rst a -> _: FStar.Monotonic.HyperStack.mem -> _: a -> Prims.GTot Type0 | [] | LowStar.Regional.rg_inv | {
"file_name": "ulib/LowStar.Regional.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | rg: LowStar.Regional.regional rst a -> _: FStar.Monotonic.HyperStack.mem -> _: a -> Prims.GTot Type0 | {
"end_col": 15,
"end_line": 139,
"start_col": 2,
"start_line": 139
} |
|
FStar.HyperStack.ST.ST | val rg_free (#a #rst: _) (rg: regional rst a) (v: a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 -> modifies (loc_all_regions_from false (Rgl?.region_of rg v)) h0 h1)) | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rg_free #a #rst (rg:regional rst a) (v:a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 ->
modifies (loc_all_regions_from false (Rgl?.region_of rg v)) h0 h1))
= (Rgl?.r_free rg) (Rgl?.state rg) v | val rg_free (#a #rst: _) (rg: regional rst a) (v: a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 -> modifies (loc_all_regions_from false (Rgl?.region_of rg v)) h0 h1))
let rg_free #a #rst (rg: regional rst a) (v: a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 -> modifies (loc_all_regions_from false (Rgl?.region_of rg v)) h0 h1)) = | true | null | false | (Rgl?.r_free rg) (Rgl?.state rg) v | {
"checked_file": "LowStar.Regional.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "LowStar.Regional.fst"
} | [] | [
"LowStar.Regional.regional",
"LowStar.Regional.__proj__Rgl__item__r_free",
"LowStar.Regional.__proj__Rgl__item__state",
"Prims.unit",
"FStar.Monotonic.HyperStack.mem",
"LowStar.Regional.rg_inv",
"LowStar.Monotonic.Buffer.modifies",
"LowStar.Monotonic.Buffer.loc_all_regions_from",
"LowStar.Regional.__proj__Rgl__item__region_of"
] | [] | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module LowStar.Regional
(**
* This module defines what is conceptually a typeclass called
* `regional` (although it is not syntactically marked as a `class`
* yet).
*
* `regional a` is the the class of types whose values have explicit
* memory allocations confined spatially within a single heap region
* in the hyperstack memory model.
*
* Being confined to a region, values of regional types support a
* natural framing principles: state mutations that do not overlap
* with a regional value's region are noninterfering.
*
* Instances of regional types are given for buffers and vectors:
* See LowStar.Regional.Instances, LowStar.RVector for samples.
*
*)
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
/// Regionality
/// Motivation: we want to ensure that all stateful operations for a value of
/// type `a` are within the `region_of` the value.
///
/// Furthermore, we would like regional to be parameterized over another type class
/// that elements can have. However, we are also trying to extract to C, meaning
/// that we can't incur any run-time lookups and indirections. In essence, we'd
/// like for members of a regional to potentially be partial applications where
/// the first argument may be an agility parameter, an extra type class for the
/// elements, etc. etc. except that partial applications are not allowed in C.
///
/// We therefore add an "st" type, which is a piece of (pure) state (hence more
/// like a parameter) that functions are allowed to capture. Currently, only
/// ``r_alloc`` needs that extra parameter. The parameter is stored within the
/// type class, so that clients are not required to manage that piece of state
/// themselves. This is, in effect, closure-conversion for ``r_alloc`` where the
/// closure state is lifted and stored in the regional itself. As such, the only
/// piece of state that ``r_alloc`` may receive is the exact value that was
/// captured.
///
/// Several alternative designs exist, e.g. making ``a`` at type ``st -> Type0``
/// (won't extract); instantiating ``st`` as a singleton type and dropping the
/// refinement (also works, but doesn't make the intent of closure-conversion
/// explicit); dropping the refinement and leaving it up to the user to store
/// the refinement in ``r_inv`` (which would then take ``state`` as an
/// argument)...
noeq type regional (st:Type) (a:Type0) =
| Rgl:
// This is not really a piece of state, but more like a parameter.
state: st ->
// The target type should have a region where it belongs.
region_of: (a -> GTot HS.rid) ->
//loc_of for the underlying a
loc_of: (a -> GTot loc) ->
// A parameterless value of type `a`.
// It does not have to satisfy the invariant `r_inv` described below.
dummy: a ->
// An invariant we want to maintain for each operation.
// For example, it may include `live` and `freeable` properties
// for related objects.
r_inv: (HS.mem -> a -> GTot Type0) ->
r_inv_reg:
(h:HS.mem -> v:a ->
Lemma (requires (r_inv h v))
(ensures (HS.live_region h (region_of v)))) ->
// A representation type of `a` and a corresponding conversion function
repr: Type0 ->
r_repr: (h:HS.mem -> v:a{r_inv h v} -> GTot repr) ->
// A core separation lemma, saying that the invariant and representation are
// preserved when an orthogonal state transition happens.
r_sep:
(v:a -> p:loc -> h:HS.mem -> h':HS.mem ->
Lemma (requires (r_inv h v /\
loc_disjoint (loc_all_regions_from false (region_of v)) p /\
modifies p h h'))
(ensures (r_inv h' v /\ r_repr h v == r_repr h' v))) ->
/// Allocation
// The representation for the initial value of type `a`
irepr: Ghost.erased repr ->
// A property that should hold for all initial values of type `a`.
r_alloc_p: (a -> GTot Type0) ->
// An allocation operation. We might have several ways of initializing a
// given target type `a`; then multiple typeclass instances should be
// defined, and each of them can be used properly.
r_alloc: ((s:st { s == state }) -> r:HST.erid ->
HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (loc_of v) h0 h1 /\ //the underlying loc is fresh
r_alloc_p v /\ r_inv h1 v /\ region_of v == r /\
r_repr h1 v == Ghost.reveal irepr))) ->
// Destruction: note that it allows to `modify` all the regions, including
// its subregions. It is fair when we want to `free` a vector and its
// elements as well, assuming the elements belong to subregions.
r_free: ((s:st { s == state }) -> v:a ->
HST.ST unit
(requires (fun h0 -> r_inv h0 v))
(ensures (fun h0 _ h1 ->
modifies (loc_all_regions_from false (region_of v)) h0 h1))) ->
regional st a
let rg_inv #a #rst (rg: regional rst a) =
Rgl?.r_inv rg
inline_for_extraction
let rg_dummy #a #rst (rg:regional rst a)
: Tot a
= Rgl?.dummy rg
inline_for_extraction
let rg_alloc #a #rst (rg:regional rst a) (r:HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (Rgl?.loc_of rg v) h0 h1 /\
(Rgl?.r_alloc_p rg) v /\ rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg)))
= Rgl?.r_alloc rg (Rgl?.state rg) r
inline_for_extraction
let rg_free #a #rst (rg:regional rst a) (v:a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 -> | false | false | LowStar.Regional.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val rg_free (#a #rst: _) (rg: regional rst a) (v: a)
: HST.ST unit
(requires (fun h0 -> rg_inv rg h0 v))
(ensures (fun h0 _ h1 -> modifies (loc_all_regions_from false (Rgl?.region_of rg v)) h0 h1)) | [] | LowStar.Regional.rg_free | {
"file_name": "ulib/LowStar.Regional.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | rg: LowStar.Regional.regional rst a -> v: a -> FStar.HyperStack.ST.ST Prims.unit | {
"end_col": 36,
"end_line": 165,
"start_col": 2,
"start_line": 165
} |
FStar.HyperStack.ST.ST | val rg_alloc (#a #rst: _) (rg: regional rst a) (r: HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures
(fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0)) (Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\ fresh_loc (Rgl?.loc_of rg v) h0 h1 /\ (Rgl?.r_alloc_p rg) v /\
rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg))) | [
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "LowStar.Modifies",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowStar",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | false | let rg_alloc #a #rst (rg:regional rst a) (r:HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (Rgl?.loc_of rg v) h0 h1 /\
(Rgl?.r_alloc_p rg) v /\ rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg)))
= Rgl?.r_alloc rg (Rgl?.state rg) r | val rg_alloc (#a #rst: _) (rg: regional rst a) (r: HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures
(fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0)) (Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\ fresh_loc (Rgl?.loc_of rg v) h0 h1 /\ (Rgl?.r_alloc_p rg) v /\
rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg)))
let rg_alloc #a #rst (rg: regional rst a) (r: HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures
(fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0)) (Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\ fresh_loc (Rgl?.loc_of rg v) h0 h1 /\ (Rgl?.r_alloc_p rg) v /\
rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg))) = | true | null | false | Rgl?.r_alloc rg (Rgl?.state rg) r | {
"checked_file": "LowStar.Regional.fst.checked",
"dependencies": [
"prims.fst.checked",
"LowStar.Modifies.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Map.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "LowStar.Regional.fst"
} | [] | [
"LowStar.Regional.regional",
"FStar.HyperStack.ST.erid",
"LowStar.Regional.__proj__Rgl__item__r_alloc",
"LowStar.Regional.__proj__Rgl__item__state",
"FStar.Monotonic.HyperStack.mem",
"Prims.l_True",
"Prims.l_and",
"FStar.Set.subset",
"FStar.Monotonic.HyperHeap.rid",
"FStar.Map.domain",
"FStar.Monotonic.Heap.heap",
"FStar.Monotonic.HyperStack.get_hmap",
"LowStar.Monotonic.Buffer.modifies",
"LowStar.Monotonic.Buffer.loc_none",
"LowStar.Monotonic.Buffer.fresh_loc",
"LowStar.Regional.__proj__Rgl__item__loc_of",
"LowStar.Regional.__proj__Rgl__item__r_alloc_p",
"LowStar.Regional.rg_inv",
"Prims.eq2",
"LowStar.Regional.__proj__Rgl__item__region_of",
"LowStar.Regional.__proj__Rgl__item__repr",
"LowStar.Regional.__proj__Rgl__item__r_repr",
"FStar.Ghost.reveal",
"LowStar.Regional.__proj__Rgl__item__irepr"
] | [] | (*
Copyright 2008-2018 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module LowStar.Regional
(**
* This module defines what is conceptually a typeclass called
* `regional` (although it is not syntactically marked as a `class`
* yet).
*
* `regional a` is the the class of types whose values have explicit
* memory allocations confined spatially within a single heap region
* in the hyperstack memory model.
*
* Being confined to a region, values of regional types support a
* natural framing principles: state mutations that do not overlap
* with a regional value's region are noninterfering.
*
* Instances of regional types are given for buffers and vectors:
* See LowStar.Regional.Instances, LowStar.RVector for samples.
*
*)
open LowStar.Modifies
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
/// Regionality
/// Motivation: we want to ensure that all stateful operations for a value of
/// type `a` are within the `region_of` the value.
///
/// Furthermore, we would like regional to be parameterized over another type class
/// that elements can have. However, we are also trying to extract to C, meaning
/// that we can't incur any run-time lookups and indirections. In essence, we'd
/// like for members of a regional to potentially be partial applications where
/// the first argument may be an agility parameter, an extra type class for the
/// elements, etc. etc. except that partial applications are not allowed in C.
///
/// We therefore add an "st" type, which is a piece of (pure) state (hence more
/// like a parameter) that functions are allowed to capture. Currently, only
/// ``r_alloc`` needs that extra parameter. The parameter is stored within the
/// type class, so that clients are not required to manage that piece of state
/// themselves. This is, in effect, closure-conversion for ``r_alloc`` where the
/// closure state is lifted and stored in the regional itself. As such, the only
/// piece of state that ``r_alloc`` may receive is the exact value that was
/// captured.
///
/// Several alternative designs exist, e.g. making ``a`` at type ``st -> Type0``
/// (won't extract); instantiating ``st`` as a singleton type and dropping the
/// refinement (also works, but doesn't make the intent of closure-conversion
/// explicit); dropping the refinement and leaving it up to the user to store
/// the refinement in ``r_inv`` (which would then take ``state`` as an
/// argument)...
noeq type regional (st:Type) (a:Type0) =
| Rgl:
// This is not really a piece of state, but more like a parameter.
state: st ->
// The target type should have a region where it belongs.
region_of: (a -> GTot HS.rid) ->
//loc_of for the underlying a
loc_of: (a -> GTot loc) ->
// A parameterless value of type `a`.
// It does not have to satisfy the invariant `r_inv` described below.
dummy: a ->
// An invariant we want to maintain for each operation.
// For example, it may include `live` and `freeable` properties
// for related objects.
r_inv: (HS.mem -> a -> GTot Type0) ->
r_inv_reg:
(h:HS.mem -> v:a ->
Lemma (requires (r_inv h v))
(ensures (HS.live_region h (region_of v)))) ->
// A representation type of `a` and a corresponding conversion function
repr: Type0 ->
r_repr: (h:HS.mem -> v:a{r_inv h v} -> GTot repr) ->
// A core separation lemma, saying that the invariant and representation are
// preserved when an orthogonal state transition happens.
r_sep:
(v:a -> p:loc -> h:HS.mem -> h':HS.mem ->
Lemma (requires (r_inv h v /\
loc_disjoint (loc_all_regions_from false (region_of v)) p /\
modifies p h h'))
(ensures (r_inv h' v /\ r_repr h v == r_repr h' v))) ->
/// Allocation
// The representation for the initial value of type `a`
irepr: Ghost.erased repr ->
// A property that should hold for all initial values of type `a`.
r_alloc_p: (a -> GTot Type0) ->
// An allocation operation. We might have several ways of initializing a
// given target type `a`; then multiple typeclass instances should be
// defined, and each of them can be used properly.
r_alloc: ((s:st { s == state }) -> r:HST.erid ->
HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (loc_of v) h0 h1 /\ //the underlying loc is fresh
r_alloc_p v /\ r_inv h1 v /\ region_of v == r /\
r_repr h1 v == Ghost.reveal irepr))) ->
// Destruction: note that it allows to `modify` all the regions, including
// its subregions. It is fair when we want to `free` a vector and its
// elements as well, assuming the elements belong to subregions.
r_free: ((s:st { s == state }) -> v:a ->
HST.ST unit
(requires (fun h0 -> r_inv h0 v))
(ensures (fun h0 _ h1 ->
modifies (loc_all_regions_from false (region_of v)) h0 h1))) ->
regional st a
let rg_inv #a #rst (rg: regional rst a) =
Rgl?.r_inv rg
inline_for_extraction
let rg_dummy #a #rst (rg:regional rst a)
: Tot a
= Rgl?.dummy rg
inline_for_extraction
let rg_alloc #a #rst (rg:regional rst a) (r:HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures (fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0))
(Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\
fresh_loc (Rgl?.loc_of rg v) h0 h1 /\
(Rgl?.r_alloc_p rg) v /\ rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\ | false | false | LowStar.Regional.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val rg_alloc (#a #rst: _) (rg: regional rst a) (r: HST.erid)
: HST.ST a
(requires (fun h0 -> True))
(ensures
(fun h0 v h1 ->
Set.subset (Map.domain (HS.get_hmap h0)) (Map.domain (HS.get_hmap h1)) /\
modifies loc_none h0 h1 /\ fresh_loc (Rgl?.loc_of rg v) h0 h1 /\ (Rgl?.r_alloc_p rg) v /\
rg_inv rg h1 v /\ (Rgl?.region_of rg) v == r /\
(Rgl?.r_repr rg) h1 v == Ghost.reveal (Rgl?.irepr rg))) | [] | LowStar.Regional.rg_alloc | {
"file_name": "ulib/LowStar.Regional.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | rg: LowStar.Regional.regional rst a -> r: FStar.HyperStack.ST.erid -> FStar.HyperStack.ST.ST a | {
"end_col": 35,
"end_line": 157,
"start_col": 2,
"start_line": 157
} |
Prims.Tot | val bitfield_eq64_lhs (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64}) : Tot U64.t | [
{
"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
}
] | 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 | 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 = | false | null | false | x `U64.logand` (bitfield_mask64 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt64.logand",
"LowParse.BitFields.bitfield_mask64"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val bitfield_eq64_lhs (x: U64.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 64}) : Tot U64.t | [] | LowParse.BitFields.bitfield_eq64_lhs | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt64.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 64} -> FStar.UInt64.t | {
"end_col": 38,
"end_line": 927,
"start_col": 2,
"start_line": 927
} |
FStar.Pervasives.Lemma | 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)))) | [
{
"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
}
] | 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) | 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)))) = | false | null | true | nth_pow2_minus_one' #n m (n - 1 - i) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 /\ | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)))) | [] | LowParse.BitFields.nth_pow2_minus_one | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 38,
"end_line": 78,
"start_col": 2,
"start_line": 78
} |
FStar.Pervasives.Lemma | val get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0) | [
{
"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
}
] | 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
) | 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) = | false | null | true | eq_nth (get_bitfield x i i)
0
(fun j ->
nth_get_bitfield x i i j;
nth_zero tot j) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_bitfield_empty
(#tot: pos)
(x: U.uint_t tot)
(i: nat { i <= tot })
: Lemma
(get_bitfield x i i == 0) | [] | LowParse.BitFields.get_bitfield_empty | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt.uint_t tot -> i: Prims.nat{i <= tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.get_bitfield x i i == 0) | {
"end_col": 3,
"end_line": 442,
"start_col": 2,
"start_line": 439
} |
Prims.Tot | val nth (#n: pos) (a: U.uint_t n) (i: nat{i < n}) : Tot bool | [
{
"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
}
] | false | let nth (#n: pos) (a: U.uint_t n) (i: nat {i < n}) : Tot bool =
U.nth a (n - 1 - i) | 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 = | false | null | false | U.nth a (n - 1 - i) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"total"
] | [
"Prims.pos",
"FStar.UInt.uint_t",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt.nth",
"Prims.op_Subtraction",
"Prims.bool"
] | [] | 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 *) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val nth (#n: pos) (a: U.uint_t n) (i: nat{i < n}) : Tot bool | [] | LowParse.BitFields.nth | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | a: FStar.UInt.uint_t n -> i: Prims.nat{i < n} -> Prims.bool | {
"end_col": 21,
"end_line": 49,
"start_col": 2,
"start_line": 49
} |
Prims.Tot | val bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) | [
{
"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
}
] | 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) | 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) = | false | null | false | [@@ inline_let ]let _ =
M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo
in
normalize_term ((pow2 (hi - lo) - 1) * pow2 lo) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | module LowParse.BitFields
module U = FStar.UInt
module M = LowParse.Math
open FStar.Mul
inline_for_extraction | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bitfield_mask (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot}) : Tot (U.uint_t tot) | [] | LowParse.BitFields.bitfield_mask | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot} -> FStar.UInt.uint_t tot | {
"end_col": 49,
"end_line": 13,
"start_col": 2,
"start_line": 9
} |
FStar.Pervasives.Lemma | val get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0) | [
{
"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
}
] | 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)
) | 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) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_bitfield_zero
(tot: pos)
(lo: nat) (hi: nat { lo <= hi /\ hi <= tot })
: Lemma
(get_bitfield #tot 0 lo hi == 0) | [] | LowParse.BitFields.get_bitfield_zero | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 3,
"end_line": 422,
"start_col": 2,
"start_line": 417
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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
) | 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) = | false | null | true | eq_nth (set_bitfield x i i y) x (fun j -> nth_set_bitfield x i i y j) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.set_bitfield_empty | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 3,
"end_line": 403,
"start_col": 2,
"start_line": 401
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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
) | 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)) = | false | null | true | 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) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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)) | [] | LowParse.BitFields.get_bitfield_raw_eq_logand_pow2_hi_lo_minus_one | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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))) | {
"end_col": 3,
"end_line": 156,
"start_col": 2,
"start_line": 150
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | U64.lognot (bitfield_mask64 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.not_bitfield_mask64 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 36,
"end_line": 907,
"start_col": 2,
"start_line": 907
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 | 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)) = | false | null | true | bitfield_mask_eq_2 tot lo hi | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.set_bitfield_eq | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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)) | {
"end_col": 30,
"end_line": 872,
"start_col": 2,
"start_line": 872
} |
FStar.Pervasives.Lemma | val bitfield_mask_lt_pow2_hi (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (bitfield_mask tot lo hi < pow2 hi) | [
{
"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
}
] | 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 | 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) = | false | null | true | M.pow2_le_compat tot hi;
M.pow2_plus (hi - lo) lo | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val bitfield_mask_lt_pow2_hi (tot: pos) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Lemma (bitfield_mask tot lo hi < pow2 hi) | [] | LowParse.BitFields.bitfield_mask_lt_pow2_hi | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 28,
"end_line": 270,
"start_col": 4,
"start_line": 269
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 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 | 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)) = | false | null | true | 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) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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)) | [] | LowParse.BitFields.get_bitfield_eq | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 5,
"end_line": 832,
"start_col": 2,
"start_line": 811
} |
FStar.Pervasives.Lemma | 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')) | [
{
"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
}
] | 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 begin
nth_set_bitfield x lo hi v (i + lo')
end
) | 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')) = | false | null | true | 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')) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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')) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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')) | [] | LowParse.BitFields.get_bitfield_set_bitfield_other | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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') | {
"end_col": 3,
"end_line": 360,
"start_col": 2,
"start_line": 353
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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 | 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) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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)) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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) | [
"recursion"
] | LowParse.BitFields.get_bitfield_partition' | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 45,
"end_line": 694,
"start_col": 2,
"start_line": 690
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 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)) | 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)) = | false | null | true | 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)) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.nth_set_bitfield | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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)) | {
"end_col": 71,
"end_line": 328,
"start_col": 1,
"start_line": 309
} |
FStar.Pervasives.Lemma | val pow2_m_minus_one_eq (n m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) | [
{
"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
}
] | false | 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) | 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 m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val pow2_m_minus_one_eq (n m: nat)
: Lemma (requires (m <= n)) (ensures ((pow2 n - 1) / pow2 m == pow2 (n - m) - 1)) | [] | LowParse.BitFields.pow2_m_minus_one_eq | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 64,
"end_line": 844,
"start_col": 2,
"start_line": 842
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | (x `U64.logand` (bitfield_mask64 lo hi)) `u64_shift_right` (U32.uint_to_t lo) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.get_bitfield64 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 77,
"end_line": 903,
"start_col": 2,
"start_line": 903
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | U8.lognot (bitfield_mask8 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.not_bitfield_mask8 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 34,
"end_line": 1164,
"start_col": 2,
"start_line": 1164
} |
Prims.Tot | val uint64 : uint_t 64 U64.t | [
{
"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
}
] | 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);
} | val uint64 : uint_t 64 U64.t
let uint64:uint_t 64 U64.t = | false | null | false | {
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)
} | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val uint64 : uint_t 64 U64.t | [] | LowParse.BitFields.uint64 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | LowParse.BitFields.uint_t 64 FStar.UInt64.t | {
"end_col": 67,
"end_line": 1215,
"start_col": 2,
"start_line": 1205
} |
FStar.Pervasives.Lemma | 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))) | [
{
"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
}
] | 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 | 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 = | false | null | true | 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 | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) /\ | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [
"recursion"
] | LowParse.BitFields.nth_pow2_minus_one' | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 46,
"end_line": 44,
"start_col": 36,
"start_line": 38
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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 | 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)}) = | false | null | false | if amount = 32ul then 0ul else x `U32.shift_right` amount | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.u32_shift_right | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 59,
"end_line": 974,
"start_col": 2,
"start_line": 974
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 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
) | 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)) = | false | null | true | 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))) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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)) | [] | LowParse.BitFields.get_bitfield_size | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 3,
"end_line": 730,
"start_col": 2,
"start_line": 715
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | (x `U32.logand` (bitfield_mask32 lo hi)) `u32_shift_right` (U32.uint_to_t lo) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.get_bitfield32 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 77,
"end_line": 980,
"start_col": 2,
"start_line": 980
} |
Prims.Tot | val uint8 : uint_t 8 U8.t | [
{
"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
}
] | 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);
} | val uint8 : uint_t 8 U8.t
let uint8:uint_t 8 U8.t = | false | null | false | {
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)
} | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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
noextract | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val uint8 : uint_t 8 U8.t | [] | LowParse.BitFields.uint8 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | LowParse.BitFields.uint_t 8 FStar.UInt8.t | {
"end_col": 66,
"end_line": 1272,
"start_col": 2,
"start_line": 1262
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 begin
nth_set_bitfield #tot1 x lo hi v i;
if lo <= i && i < hi
then nth_size tot1 tot2 v (i - lo)
end
) | 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)) = | false | null | true | 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))) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.set_bitfield_size | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 3,
"end_line": 753,
"start_col": 2,
"start_line": 740
} |
Prims.Tot | val bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == bitfield_mask 32 lo hi}) | [
{
"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
}
] | 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 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 | 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}) = | false | null | false | 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)) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val bitfield_mask32 (lo: nat) (hi: nat{lo <= hi /\ hi <= 32})
: Tot (x: U32.t{U32.v x == bitfield_mask 32 lo hi}) | [] | LowParse.BitFields.bitfield_mask32 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 5,
"end_line": 967,
"start_col": 2,
"start_line": 962
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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 | 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)}) = | false | null | false | if amount = 16ul then 0us else x `U16.shift_left` amount | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.u16_shift_left | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 58,
"end_line": 1068,
"start_col": 2,
"start_line": 1068
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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 | 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)}) = | false | null | false | if amount = 16ul then 0us else x `U16.shift_right` amount | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.u16_shift_right | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 59,
"end_line": 1051,
"start_col": 2,
"start_line": 1051
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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 | 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)}) = | false | null | false | let y = if amount = 8ul then 0uy else x `U8.shift_right` amount in
y | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.u8_shift_right | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 3,
"end_line": 1127,
"start_col": 1,
"start_line": 1124
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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 | 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)}) = | false | null | false | let y = if amount = 8ul then 0uy else x `U8.shift_left` amount in
y | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.u8_shift_left | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 3,
"end_line": 1174,
"start_col": 1,
"start_line": 1171
} |
FStar.Pervasives.Lemma | val 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) | [
{
"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
}
] | false | 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) | val 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)
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) = | false | null | true | 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) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.pos",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.modulo_lemma",
"FStar.Mul.op_Star",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"FStar.Math.Lemmas.swap_mul",
"LowParse.Math.lemma_mult_lt'",
"LowParse.Math.lemma_mult_nat",
"FStar.Math.Lemmas.pow2_plus",
"FStar.Math.Lemmas.pow2_le_compat",
"FStar.UInt.shift_left_value_lemma",
"Prims.l_True",
"Prims.squash",
"Prims.op_LessThan",
"Prims.eq2",
"FStar.UInt.uint_t",
"LowParse.BitFields.bitfield_mask",
"FStar.UInt.shift_left",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | 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
) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.bitfield_mask_eq | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | tot: Prims.pos -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.Pervasives.Lemma
(ensures
0 <= Prims.pow2 (hi - lo) - 1 /\ Prims.pow2 (hi - lo) - 1 < Prims.pow2 tot /\
LowParse.BitFields.bitfield_mask tot lo hi ==
FStar.UInt.shift_left (Prims.pow2 (hi - lo) - 1) lo) | {
"end_col": 60,
"end_line": 30,
"start_col": 2,
"start_line": 22
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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) | 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)}) = | false | null | false | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.get_bitfield_gen64 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) } | {
"end_col": 95,
"end_line": 948,
"start_col": 2,
"start_line": 947
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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);
(* 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 | 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)}) = | false | null | false | 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 | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.get_bitfield_gen8 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) } | {
"end_col": 5,
"end_line": 1137,
"start_col": 2,
"start_line": 1133
} |
FStar.Pervasives.Lemma | 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' ) | [
{
"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
}
] | 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 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
) | 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' = | false | null | true | 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)))) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"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"
} | null | 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' ) | [] | LowParse.BitFields.set_bitfield_set_bitfield_get_bitfield | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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')) | {
"end_col": 3,
"end_line": 797,
"start_col": 2,
"start_line": 776
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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);
(* 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 | 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)}) = | false | null | false | 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 | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.set_bitfield_gen8 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) } | {
"end_col": 5,
"end_line": 1153,
"start_col": 2,
"start_line": 1144
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | (x `U16.logand` (bitfield_mask16 lo hi)) `u16_shift_right` (U32.uint_to_t lo) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.get_bitfield16 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 77,
"end_line": 1057,
"start_col": 2,
"start_line": 1057
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 begin
nth_pow2_minus_one #tot (hi - lo) (i - lo)
end | 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)) = | false | null | true | 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) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.nth_bitfield_mask | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 7,
"end_line": 106,
"start_col": 4,
"start_line": 100
} |
Prims.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) | [
{
"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
}
] | 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 | 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) = | false | null | false | (x `U.logand` (bitfield_mask tot lo hi)) `U.shift_right` lo | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_bitfield_raw (#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat{lo <= hi /\ hi <= tot})
: Tot (U.uint_t tot) | [] | LowParse.BitFields.get_bitfield_raw | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> FStar.UInt.uint_t tot | {
"end_col": 59,
"end_line": 109,
"start_col": 2,
"start_line": 109
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | false | 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 | 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 b: U.uint_t n) (f: (i: nat{i < n} -> Lemma (nth a i == nth b i)))
: Lemma (a == b) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.eq_nth | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 17,
"end_line": 69,
"start_col": 1,
"start_line": 62
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 | 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)) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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)) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.nth_le_pow2_m | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 29,
"end_line": 192,
"start_col": 2,
"start_line": 189
} |
FStar.Pervasives.Lemma | 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))) | [
{
"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
}
] | 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 begin
nth_logand x (bitfield_mask tot lo hi) (i + lo);
nth_bitfield_mask tot lo hi (i + lo)
end else
() | 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))) = | false | null | true | 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)) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | LowParse.BitFields.nth_get_bitfield_raw | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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))) | {
"end_col": 6,
"end_line": 143,
"start_col": 2,
"start_line": 137
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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)) | 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) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.bitfield_mask_eq_2 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 65,
"end_line": 866,
"start_col": 2,
"start_line": 863
} |
Prims.Tot | val get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo)) | [
{
"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
}
] | 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 | 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)) = | false | null | false | get_bitfield_raw_bounded x lo hi;
get_bitfield_raw x lo hi | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_bitfield
(#tot: pos) (x: U.uint_t tot) (lo: nat) (hi: nat {lo <= hi /\ hi <= tot})
: Tot (ubitfield tot (hi - lo)) | [] | LowParse.BitFields.get_bitfield | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt.uint_t tot -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= tot}
-> LowParse.BitFields.ubitfield tot (hi - lo) | {
"end_col": 26,
"end_line": 206,
"start_col": 2,
"start_line": 205
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 | 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)) = | false | null | true | nth_lognot (bitfield_mask tot lo hi) i;
nth_bitfield_mask tot lo hi i | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.nth_not_bitfield_mask | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 31,
"end_line": 295,
"start_col": 2,
"start_line": 294
} |
FStar.Pervasives.Lemma | 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))) | [
{
"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
}
] | 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 | 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))) = | false | null | true | nth_get_bitfield_raw x lo hi i | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | LowParse.BitFields.nth_get_bitfield | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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))) | {
"end_col": 32,
"end_line": 210,
"start_col": 2,
"start_line": 210
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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 | 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) = | false | null | true | M.pow2_multiplication_modulo_lemma_1 (pow2 (hi - lo) - 1) m lo | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.bitfield_mask_mod_pow2_lo | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 64,
"end_line": 262,
"start_col": 2,
"start_line": 262
} |
Prims.Tot | val bitfield_eq32_lhs (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32}) : Tot U32.t | [
{
"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
}
] | 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 | 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 = | false | null | false | x `U32.logand` (bitfield_mask32 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt32.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.logand",
"LowParse.BitFields.bitfield_mask32"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val bitfield_eq32_lhs (x: U32.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 32}) : Tot U32.t | [] | LowParse.BitFields.bitfield_eq32_lhs | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt32.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 32} -> FStar.UInt32.t | {
"end_col": 38,
"end_line": 1004,
"start_col": 2,
"start_line": 1004
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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)) | 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)) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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)) | [] | LowParse.BitFields.get_bitfield_raw_bounded | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)) | {
"end_col": 35,
"end_line": 200,
"start_col": 2,
"start_line": 198
} |
FStar.Pervasives.Lemma | val nth_zero (tot: pos) (i: nat{i < tot}) : Lemma (nth #tot 0 i == false) | [
{
"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
}
] | false | let nth_zero
(tot: pos)
(i: nat {i < tot})
: Lemma
(nth #tot 0 i == false)
= U.zero_nth_lemma #tot i | 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) = | false | null | true | U.zero_nth_lemma #tot i | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val nth_zero (tot: pos) (i: nat{i < tot}) : Lemma (nth #tot 0 i == false) | [] | LowParse.BitFields.nth_zero | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | tot: Prims.pos -> i: Prims.nat{i < tot}
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.nth 0 i == false) | {
"end_col": 25,
"end_line": 410,
"start_col": 2,
"start_line": 410
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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) | 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)}) = | false | null | false | (x `U64.logand` (not_bitfield_mask64 lo hi)) `U64.logor` (v `u64_shift_left` (U32.uint_to_t lo)) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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)}) | [] | LowParse.BitFields.set_bitfield64 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) } | {
"end_col": 94,
"end_line": 921,
"start_col": 2,
"start_line": 921
} |
Prims.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) | [
{
"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
}
] | 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) | 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) = | false | null | false | (x `U.logand` (not_bitfield_mask tot lo hi)) `U.logor` (v `U.shift_left` lo) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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)) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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) | [] | LowParse.BitFields.set_bitfield | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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 | {
"end_col": 76,
"end_line": 301,
"start_col": 2,
"start_line": 301
} |
FStar.Pervasives.Lemma | val set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y) | [
{
"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
}
] | 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
) | 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) = | false | null | true | eq_nth (set_bitfield x 0 tot y) y (fun i -> nth_set_bitfield x 0 tot y i) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val set_bitfield_full
(#tot: pos) (x: U.uint_t tot) (y: ubitfield tot tot)
: Lemma
(set_bitfield x 0 tot y == y) | [] | LowParse.BitFields.set_bitfield_full | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 3,
"end_line": 395,
"start_col": 2,
"start_line": 393
} |
FStar.Pervasives.Lemma | val get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x) | [
{
"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
}
] | 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
) | 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) = | false | null | true | eq_nth (get_bitfield x 0 tot) x (fun i -> nth_get_bitfield x 0 tot i) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_bitfield_full
(#tot: pos)
(x: U.uint_t tot)
: Lemma
(get_bitfield x 0 tot == x) | [] | LowParse.BitFields.get_bitfield_full | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt.uint_t tot
-> FStar.Pervasives.Lemma (ensures LowParse.BitFields.get_bitfield x 0 tot == x) | {
"end_col": 3,
"end_line": 431,
"start_col": 2,
"start_line": 429
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | let get_bitfield_partition = 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))
let get_bitfield_partition = | false | null | true | get_bitfield_partition' | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"lemma"
] | [
"LowParse.BitFields.get_bitfield_partition'"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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)) | [] | LowParse.BitFields.get_bitfield_partition | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 52,
"end_line": 696,
"start_col": 29,
"start_line": 696
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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
) | 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)) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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')) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | LowParse.BitFields.set_bitfield_set_bitfield_other | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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) | {
"end_col": 3,
"end_line": 387,
"start_col": 2,
"start_line": 382
} |
FStar.Pervasives.Lemma | 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')) | [
{
"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
}
] | 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
) | 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')) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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')) | [] | LowParse.BitFields.set_bitfield_set_bitfield_same_gen | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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') | {
"end_col": 3,
"end_line": 373,
"start_col": 2,
"start_line": 369
} |
Prims.Tot | 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)}) | [
{
"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
}
] | 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) | 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)}) = | false | null | false | [@@ 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)}) | [] | LowParse.BitFields.not_bitfield_mask | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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)} | {
"end_col": 24,
"end_line": 290,
"start_col": 2,
"start_line": 275
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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 | 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}) = | false | null | false | [@@ 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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) }) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.bitfield_eq16_rhs | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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 } | {
"end_col": 37,
"end_line": 1091,
"start_col": 2,
"start_line": 1088
} |
FStar.Pervasives.Lemma | 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
)) | [
{
"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
}
] | 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 | 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)) = | false | null | true | get_bitfield_partition_2_gen 0 mid tot x y;
get_bitfield_full x;
get_bitfield_full y | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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
)) | [] | LowParse.BitFields.get_bitfield_partition_2 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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) | {
"end_col": 21,
"end_line": 678,
"start_col": 2,
"start_line": 676
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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
) | 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)
) = | false | null | true | 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) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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) | [] | LowParse.BitFields.get_bitfield_logxor | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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)) | {
"end_col": 3,
"end_line": 230,
"start_col": 2,
"start_line": 226
} |
Prims.Tot | val bitfield_eq16_lhs (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16}) : Tot U16.t | [
{
"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
}
] | 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 | 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 = | false | null | false | x `U16.logand` (bitfield_mask16 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt16.t",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt16.logand",
"LowParse.BitFields.bitfield_mask16"
] | [] | 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}) | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | val bitfield_eq16_lhs (x: U16.t) (lo: nat) (hi: nat{lo <= hi /\ hi <= 16}) : Tot U16.t | [] | LowParse.BitFields.bitfield_eq16_lhs | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | x: FStar.UInt16.t -> lo: Prims.nat -> hi: Prims.nat{lo <= hi /\ hi <= 16} -> FStar.UInt16.t | {
"end_col": 38,
"end_line": 1081,
"start_col": 2,
"start_line": 1081
} |
Prims.Tot | 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}) | [
{
"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
}
] | 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) | 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}) = | false | null | false | U32.lognot (bitfield_mask32 lo hi) | {
"checked_file": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_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"
} | null | 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}) | [] | LowParse.BitFields.not_bitfield_mask32 | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | 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} | {
"end_col": 36,
"end_line": 984,
"start_col": 2,
"start_line": 984
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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
) | 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)) = | false | null | true | 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": "LowParse.BitFields.fst.checked",
"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"
} | [
"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"
] | [] | 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 | false | false | LowParse.BitFields.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val get_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) | [] | LowParse.BitFields.get_bitfield_logor | {
"file_name": "src/lowparse/LowParse.BitFields.fst",
"git_rev": "446a08ce38df905547cf20f28c43776b22b8087a",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} |
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)) | {
"end_col": 3,
"end_line": 220,
"start_col": 2,
"start_line": 216
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.