file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.UInt128.fst | FStar.UInt128.u64_l32_mask | val u64_l32_mask:x: U64.t{U64.v x == pow2 32 - 1} | val u64_l32_mask:x: U64.t{U64.v x == pow2 32 - 1} | let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 76,
"end_line": 899,
"start_col": 0,
"start_line": 899
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t{FStar.UInt64.v x == Prims.pow2 32 - 1} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.uint_to_t"
] | [] | false | false | false | false | false | let u64_l32_mask:x: U64.t{U64.v x == pow2 32 - 1} =
| U64.uint_to_t 0xffffffff | false |
FStar.UInt128.fst | FStar.UInt128.u64_mod_32 | val u64_mod_32 (a: U64.t)
: Pure U64.t (requires True) (ensures (fun r -> U64.v r = U64.v a % pow2 32)) | val u64_mod_32 (a: U64.t)
: Pure U64.t (requires True) (ensures (fun r -> U64.v r = U64.v a % pow2 32)) | let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 905,
"start_col": 0,
"start_line": 901
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | Prims.Pure | [] | [] | [
"FStar.UInt64.t",
"FStar.UInt64.logand",
"FStar.UInt128.u64_l32_mask",
"Prims.unit",
"FStar.UInt.logand_mask",
"FStar.UInt64.n",
"FStar.UInt64.v",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.pow2"
] | [] | false | false | false | false | false | let u64_mod_32 (a: U64.t)
: Pure U64.t (requires True) (ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
| UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask | false |
FStar.UInt128.fst | FStar.UInt128.plh | val plh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | val plh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y)) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 979,
"start_col": 0,
"start_line": 978
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} = | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.mul32_bound",
"FStar.UInt128.l32",
"FStar.UInt64.v",
"FStar.UInt128.h32",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"Prims.pow2"
] | [] | false | false | false | false | false | let plh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} =
| mul32_bound (l32 (U64.v x)) (h32 (U64.v y)) | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_low | val mul_wide_low : x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 107,
"end_line": 990,
"start_col": 0,
"start_line": 990
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.op_Addition",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.pll_l",
"Prims.int"
] | [] | false | false | false | true | false | let mul_wide_low (x y: U64.t) =
| (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y | false |
|
FStar.UInt128.fst | FStar.UInt128.phl | val phl (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | val phl (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y)) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 981,
"start_col": 0,
"start_line": 980
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} = | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.mul32_bound",
"FStar.UInt128.h32",
"FStar.UInt64.v",
"FStar.UInt128.l32",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"Prims.pow2"
] | [] | false | false | false | false | false | let phl (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} =
| mul32_bound (h32 (U64.v x)) (l32 (U64.v y)) | false |
FStar.UInt128.fst | FStar.UInt128.pll | val pll (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | val pll (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y)) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 977,
"start_col": 0,
"start_line": 976
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.mul32_bound",
"FStar.UInt128.l32",
"FStar.UInt64.v",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"Prims.pow2"
] | [] | false | false | false | false | false | let pll (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} =
| mul32_bound (l32 (U64.v x)) (l32 (U64.v y)) | false |
FStar.UInt128.fst | FStar.UInt128.product_bound | val product_bound (a b: nat) (k: pos)
: Lemma (requires (a < k /\ b < k)) (ensures a * b <= k * k - 2 * k + 1) | val product_bound (a b: nat) (k: pos)
: Lemma (requires (a < k /\ b < k)) (ensures a * b <= k * k - 2 * k + 1) | let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 927,
"start_col": 0,
"start_line": 923
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Prims.nat -> b: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (requires a < k /\ b < k) (ensures a * b <= k * k - 2 * k + 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.lemma_mult_le_left",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Math.Lemmas.lemma_mult_le_right",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.squash",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let product_bound (a b: nat) (k: pos)
: Lemma (requires (a < k /\ b < k)) (ensures a * b <= k * k - 2 * k + 1) =
| Math.lemma_mult_le_right b a (k - 1);
Math.lemma_mult_le_left (k - 1) b (k - 1) | false |
FStar.UInt128.fst | FStar.UInt128.u32_product_bound | val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1) | val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1) | let u32_product_bound a b =
uint_product_bound #32 a b | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 938,
"start_col": 0,
"start_line": 937
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Prims.nat{a < Prims.pow2 32} -> b: Prims.nat{b < Prims.pow2 32}
-> FStar.Pervasives.Lemma
(ensures FStar.UInt.size (a * b) 64 /\ a * b < Prims.pow2 64 - Prims.pow2 32 - 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.pow2",
"FStar.UInt128.uint_product_bound",
"Prims.unit"
] | [] | true | false | true | false | false | let u32_product_bound a b =
| uint_product_bound #32 a b | false |
FStar.UInt128.fst | FStar.UInt128.u64_32_digits | val u64_32_digits (a: U64.t) : Lemma ((U64.v a / pow2 32) * pow2 32 + U64.v a % pow2 32 == U64.v a) | val u64_32_digits (a: U64.t) : Lemma ((U64.v a / pow2 32) * pow2 32 + U64.v a % pow2 32 == U64.v a) | let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 908,
"start_col": 0,
"start_line": 907
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
(FStar.UInt64.v a / Prims.pow2 32) * Prims.pow2 32 + FStar.UInt64.v a % Prims.pow2 32 ==
FStar.UInt64.v a) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.div_mod",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.op_Division",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let u64_32_digits (a: U64.t) : Lemma ((U64.v a / pow2 32) * pow2 32 + U64.v a % pow2 32 == U64.v a) =
| div_mod (U64.v a) (pow2 32) | false |
FStar.UInt128.fst | FStar.UInt128.gte_mask | val gte_mask: a:t -> b:t -> Tot (c:t{(v a >= v b ==> v c = pow2 n - 1) /\ (v a < v b ==> v c = 0)}) | val gte_mask: a:t -> b:t -> Tot (c:t{(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 r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; } | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 892,
"start_col": 0,
"start_line": 882
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt128.t -> b: FStar.UInt128.t
-> c:
FStar.UInt128.t
{ (FStar.UInt128.v a >= FStar.UInt128.v b ==>
FStar.UInt128.v c = Prims.pow2 FStar.UInt128.n - 1) /\
(FStar.UInt128.v a < FStar.UInt128.v b ==> FStar.UInt128.v c = 0) } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"Prims.unit",
"FStar.UInt128.lt_characterization",
"FStar.UInt128.gte_characterization",
"FStar.UInt64.t",
"FStar.UInt64.logor",
"FStar.UInt64.logand",
"FStar.UInt64.eq_mask",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt64.gte_mask",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt64.lognot",
"Prims.l_True",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt128.v",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_LessThan",
"FStar.UInt128.n"
] | [] | false | false | false | false | false | let gte_mask (a b: t)
: Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
| let mask_hi_gte =
U64.logand (U64.gte_mask a.high b.high) (U64.lognot (U64.eq_mask a.high b.high))
in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high) (U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask } | false |
FStar.UInt128.fst | FStar.UInt128.u32_combine | val u32_combine (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = (U64.v hi % pow2 32) * pow2 32 + U64.v lo)) | val u32_combine (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = (U64.v hi % pow2 32) * pow2 32 + U64.v lo)) | let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 920,
"start_col": 0,
"start_line": 917
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 30,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | Prims.Pure | [] | [] | [
"FStar.UInt64.t",
"FStar.UInt64.add",
"FStar.UInt64.shift_left",
"FStar.UInt128.u32_32",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.op_Modulus"
] | [] | false | false | false | false | false | let u32_combine (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = (U64.v hi % pow2 32) * pow2 32 + U64.v lo)) =
| U64.add lo (U64.shift_left hi u32_32) | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_high | val mul_wide_high : x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 57,
"end_line": 995,
"start_col": 0,
"start_line": 992
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"Prims.op_Addition",
"FStar.UInt128.phh",
"Prims.op_Division",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.plh",
"Prims.op_Modulus",
"Prims.int"
] | [] | false | false | false | true | false | let mul_wide_high (x y: U64.t) =
| phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32 | false |
|
FStar.UInt128.fst | FStar.UInt128.u32_32 | val u32_32:x: U32.t{U32.v x == 32} | val u32_32:x: U32.t{U32.v x == 32} | let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 54,
"end_line": 914,
"start_col": 0,
"start_line": 914
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt32.t{FStar.UInt32.v x == 32} | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt32.uint_to_t"
] | [] | false | false | false | false | false | let u32_32:x: U32.t{U32.v x == 32} =
| U32.uint_to_t 32 | false |
FStar.UInt128.fst | FStar.UInt128.uint_product_bound | val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1) | val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1) | let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 933,
"start_col": 0,
"start_line": 931
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: FStar.UInt.uint_t n -> b: FStar.UInt.uint_t n
-> FStar.Pervasives.Lemma (ensures a * b <= Prims.pow2 (2 * n) - 2 * Prims.pow2 n + 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"FStar.UInt.uint_t",
"FStar.Math.Lemmas.pow2_plus",
"Prims.unit",
"FStar.UInt128.product_bound",
"Prims.pow2"
] | [] | true | false | true | false | false | let uint_product_bound #n a b =
| product_bound a b (pow2 n);
Math.pow2_plus n n | false |
FStar.UInt128.fst | FStar.UInt128.u32_combine' | val u32_combine' (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) | val u32_combine' (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) | let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 1024,
"start_col": 0,
"start_line": 1021
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t') | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | Prims.Pure | [] | [] | [
"FStar.UInt64.t",
"FStar.UInt64.add",
"FStar.UInt64.shift_left",
"FStar.UInt128.u32_32",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt64.v",
"Prims.pow2",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Addition",
"Prims.op_Modulus",
"FStar.Mul.op_Star"
] | [] | false | false | false | false | false | let u32_combine' (hi lo: U64.t)
: Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
| U64.add lo (U64.shift_left hi u32_32) | false |
FStar.UInt128.fst | FStar.UInt128.u64_32_product | val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64) | val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64) | let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 65,
"end_line": 1068,
"start_col": 0,
"start_line": 1060
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 25,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
xl: FStar.UInt.uint_t 32 ->
xh: FStar.UInt.uint_t 32 ->
yl: FStar.UInt.uint_t 32 ->
yh: FStar.UInt.uint_t 32
-> FStar.Pervasives.Lemma
(ensures
(xl + xh * Prims.pow2 32) * (yl + yh * Prims.pow2 32) ==
xl * yl + (xl * yh) * Prims.pow2 32 + (xh * yl) * Prims.pow2 32 + (xh * yh) * Prims.pow2 64) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt.uint_t",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.unit",
"FStar.Math.Lemmas.pow2_plus",
"FStar.UInt128.mul_abc_to_acb",
"FStar.UInt128.product_sums",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual"
] | [] | true | false | true | false | false | let u64_32_product xl xh yl yh =
| assert (xh >= 0);
assert (pow2 32 >= 0);
assert (xh * pow2 32 >= 0);
product_sums xl (xh * pow2 32) yl (yh * pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64) | false |
FStar.UInt128.fst | FStar.UInt128.product_expand | val product_expand (x y: U64.t)
: Lemma
(U64.v x * U64.v y == phh x y * pow2 64 + (plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) | val product_expand (x y: U64.t)
: Lemma
(U64.v x * U64.v y == phh x y * pow2 64 + (plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) | let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y)) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 80,
"end_line": 1077,
"start_col": 0,
"start_line": 1071
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v x * FStar.UInt64.v y ==
FStar.UInt128.phh x y * Prims.pow2 64 +
(FStar.UInt128.plh x y + FStar.UInt128.phl x y + FStar.UInt128.pll_h x y) * Prims.pow2 32 +
FStar.UInt128.pll_l x y) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.u64_32_product",
"FStar.UInt128.l32",
"FStar.UInt64.v",
"FStar.UInt128.h32",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.l_True",
"Prims.squash",
"FStar.UInt128.phh",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"FStar.UInt128.pll_l",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let product_expand (x y: U64.t)
: Lemma
(U64.v x * U64.v y == phh x y * pow2 64 + (plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) =
| assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y)) | false |
FStar.UInt128.fst | FStar.UInt128.add_mod_then_mod | val add_mod_then_mod (n m: nat) (k: pos) : Lemma ((n + m % k) % k == (n + m) % k) | val add_mod_then_mod (n m: nat) (k: pos) : Lemma ((n + m % k) % k == (n + m) % k) | let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 16,
"end_line": 1089,
"start_col": 0,
"start_line": 1085
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures (n + m % k) % k == (n + m) % k) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.UInt128.mod_double",
"Prims.unit",
"FStar.UInt128.mod_add",
"Prims.op_Modulus",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let add_mod_then_mod (n m: nat) (k: pos) : Lemma ((n + m % k) % k == (n + m) % k) =
| mod_add n m k;
mod_add n (m % k) k;
mod_double m k | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_impl_t' | val mul_wide_impl_t' (x y: U64.t)
: Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures
(fun r ->
let u1, w3, x', t' = r in
U64.v u1 == U64.v x % pow2 32 /\ U64.v w3 == pll_l x y /\ U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) | val mul_wide_impl_t' (x y: U64.t)
: Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures
(fun r ->
let u1, w3, x', t' = r in
U64.v u1 == U64.v x % pow2 32 /\ U64.v w3 == pll_l x y /\ U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) | let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t') | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 18,
"end_line": 1018,
"start_col": 0,
"start_line": 998
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> Prims.Pure (((FStar.UInt64.t * FStar.UInt64.t) * FStar.UInt64.t) * FStar.UInt64.t) | Prims.Pure | [] | [] | [
"FStar.UInt64.t",
"FStar.Pervasives.Native.Mktuple4",
"FStar.UInt64.add",
"FStar.UInt64.mul",
"Prims.unit",
"FStar.UInt128.u32_product_bound",
"FStar.UInt64.v",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt64.n",
"FStar.UInt128.h32",
"FStar.UInt64.shift_right",
"FStar.UInt128.u32_32",
"FStar.UInt128.pll_h",
"FStar.UInt128.pll_l",
"FStar.UInt128.u64_mod_32",
"FStar.UInt.uint_t",
"FStar.UInt128.pll",
"FStar.Pervasives.Native.tuple4",
"Prims.l_True",
"Prims.l_and",
"Prims.op_Modulus",
"Prims.pow2",
"Prims.op_Addition",
"FStar.UInt128.phl"
] | [] | false | false | false | false | false | let mul_wide_impl_t' (x y: U64.t)
: Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures
(fun r ->
let u1, w3, x', t' = r in
U64.v u1 == U64.v x % pow2 32 /\ U64.v w3 == pll_l x y /\ U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
| let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t') | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_impl | val mul_wide_impl (x y: U64.t)
: Tot (r: t{U64.v r.low == mul_wide_low x y /\ U64.v r.high == mul_wide_high x y % pow2 64}) | val mul_wide_impl (x y: U64.t)
: Tot (r: t{U64.v r.low == mul_wide_low x y /\ U64.v r.high == mul_wide_high x y % pow2 64}) | let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 3,
"end_line": 1051,
"start_col": 0,
"start_line": 1027
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> r:
FStar.UInt128.t
{ FStar.UInt64.v (Mkuint128?.low r) == FStar.UInt128.mul_wide_low x y /\
FStar.UInt64.v (Mkuint128?.high r) == FStar.UInt128.mul_wide_high x y % Prims.pow2 64 } | Prims.Tot | [
"total"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.UInt64.v",
"Prims.op_Modulus",
"Prims.op_Addition",
"FStar.UInt128.phh",
"Prims.op_Division",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.plh",
"FStar.UInt64.add_mod",
"FStar.UInt64.add",
"FStar.UInt64.mul",
"FStar.Mul.op_Star",
"FStar.UInt128.pll_l",
"FStar.UInt128.u32_combine'",
"FStar.UInt128.mod_mul_pow2",
"FStar.UInt128.u32_product_bound",
"FStar.UInt64.shift_right",
"FStar.UInt128.u32_32",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt64.n",
"FStar.UInt128.h32",
"FStar.UInt128.u64_mod_32",
"FStar.UInt128.t",
"Prims.l_and",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.mul_wide_low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.mul_wide_high",
"FStar.Pervasives.Native.tuple4",
"FStar.UInt128.mul_wide_impl_t'"
] | [] | false | false | false | false | false | let mul_wide_impl (x y: U64.t)
: Tot (r: t{U64.v r.low == mul_wide_low x y /\ U64.v r.high == mul_wide_high x y % pow2 64}) =
| let u1, w3, x', t' = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 ==
(phh x y + (phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) %
pow2 64);
let r = { low = r0; high = r1 } in
r | false |
FStar.UInt128.fst | FStar.UInt128.mod_spec_multiply | val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k) | val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k) | let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 1135,
"start_col": 0,
"start_line": 1134
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (ensures ((n - n % k) / k) * k == n - n % k) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.lemma_mod_spec2",
"Prims.unit"
] | [] | true | false | true | false | false | let mod_spec_multiply n k =
| Math.lemma_mod_spec2 n k | false |
FStar.UInt128.fst | FStar.UInt128.shift_add | val shift_add (n: nat) (m: nat{m < pow2 32})
: Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) | val shift_add (n: nat) (m: nat{m < pow2 32})
: Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) | let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 1093,
"start_col": 0,
"start_line": 1091
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> m: Prims.nat{m < Prims.pow2 32}
-> FStar.Pervasives.Lemma
(ensures n * Prims.pow2 32 % Prims.pow2 64 + m == (n * Prims.pow2 32 + m) % Prims.pow2 64) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.pow2",
"FStar.UInt128.add_mod_small'",
"FStar.Mul.op_Star",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let shift_add (n: nat) (m: nat{m < pow2 32})
: Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
| add_mod_small' m (n * pow2 32) (pow2 64) | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_low_ok | val mul_wide_low_ok (x y: U64.t) : Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) | val mul_wide_low_ok (x y: U64.t) : Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) | let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 24,
"end_line": 1106,
"start_col": 0,
"start_line": 1095
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt128.mul_wide_low x y == FStar.UInt64.v x * FStar.UInt64.v y % Prims.pow2 64) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.product_low_expand",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.mul_wide_low",
"Prims.op_Modulus",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.pll_l",
"FStar.UInt128.shift_add",
"FStar.UInt128.mod_mul",
"FStar.UInt128.add_mod_then_mod",
"FStar.Math.Lemmas.pow2_plus",
"Prims.l_True",
"Prims.squash",
"FStar.UInt64.v",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let mul_wide_low_ok (x y: U64.t) : Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
| Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
((plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32) * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) % pow2 32) * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y | false |
FStar.UInt128.fst | FStar.UInt128.product_low_expand | val product_low_expand (x y: U64.t)
: Lemma
((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) | val product_low_expand (x y: U64.t)
: Lemma
((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) | let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 97,
"end_line": 1083,
"start_col": 0,
"start_line": 1079
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v x * FStar.UInt64.v y % Prims.pow2 64 ==
((FStar.UInt128.plh x y + FStar.UInt128.phl x y + FStar.UInt128.pll_h x y) * Prims.pow2 32 +
FStar.UInt128.pll_l x y) %
Prims.pow2 64) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.Math.Lemmas.lemma_mod_plus",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.pll_l",
"FStar.UInt128.phh",
"Prims.unit",
"FStar.UInt128.product_expand",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"FStar.UInt64.v",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let product_low_expand (x y: U64.t)
: Lemma
((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
| product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64) | false |
FStar.UInt128.fst | FStar.UInt128.product_high32 | val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y) | val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y) | let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 51,
"end_line": 1117,
"start_col": 0,
"start_line": 1111
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v x * FStar.UInt64.v y / Prims.pow2 32 ==
FStar.UInt128.phh x y * Prims.pow2 32 + FStar.UInt128.plh x y + FStar.UInt128.phl x y +
FStar.UInt128.pll_h x y) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.Math.Lemmas.small_division_lemma_1",
"FStar.UInt128.pll_l",
"Prims.pow2",
"Prims.unit",
"FStar.UInt128.mul_div_cancel",
"Prims.op_Addition",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"FStar.Mul.op_Star",
"FStar.UInt128.phh",
"FStar.Math.Lemmas.division_addition_lemma",
"FStar.UInt128.product_expand",
"FStar.Math.Lemmas.pow2_plus"
] | [] | true | false | true | false | false | let product_high32 x y =
| Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32) | false |
FStar.UInt128.fst | FStar.UInt128.mod_spec_mod | val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0) | val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0) | let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 1140,
"start_col": 0,
"start_line": 1138
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (ensures (n - n % k) % k == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.multiple_modulo_lemma",
"Prims.op_Division",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Subtraction",
"Prims.op_Modulus",
"FStar.Mul.op_Star"
] | [] | true | false | true | false | false | let mod_spec_mod n k =
| assert (n - n % k == (n / k) * k);
Math.multiple_modulo_lemma (n / k) k | false |
FStar.UInt128.fst | FStar.UInt128.sum_shift_carry | val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k) | val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k) | let sum_shift_carry a b k =
div_sum_combine a (b+a%k) k;
// assert (a / k + (b + a%k) / k == (a + b + (a % k - a % k) - (b + a%k) % k) / k);
// assert ((a + b + (a % k - a % k) - (b + a%k) % k) / k == (a + b - (b + a%k) % k) / k);
add_mod_then_mod b a k;
Math.lemma_mod_spec (a+b) k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 1194,
"start_col": 0,
"start_line": 1189
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = ()
let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k
let sub_mod_gt_0 (n:nat) (k:pos) :
Lemma (0 <= n - n % k) = ()
val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k))
#push-options "--retry 5" // sporadically fails
let sum_rounded_mod_exact n m k =
n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n%k) (m - m%k) k;
Math.div_exact_r ((n - n%k) + (m - m % k)) k
#pop-options
val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k)
#push-options "--retry 5" // sporadically fails
let div_sum_combine n m k =
sum_rounded_mod_exact n m k;
div_sum_combine1 n m k;
mul_injective (n / k + m / k) (((n - n%k) + (m - m%k)) / k) k;
assert (n + m - n % k - m % k == (n - n%k) + (m - m%k))
#pop-options
val sum_shift_carry : a:nat -> b:nat -> k:pos -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Prims.nat -> b: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures a / k + (b + a % k) / k == (a + b) / k) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.lemma_mod_spec",
"Prims.op_Addition",
"Prims.unit",
"FStar.UInt128.add_mod_then_mod",
"FStar.UInt128.div_sum_combine",
"Prims.op_Modulus"
] | [] | true | false | true | false | false | let sum_shift_carry a b k =
| div_sum_combine a (b + a % k) k;
add_mod_then_mod b a k;
Math.lemma_mod_spec (a + b) k | false |
FStar.UInt128.fst | FStar.UInt128.product_high_expand | val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32) | val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32) | let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
() | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 1129,
"start_col": 0,
"start_line": 1124
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 5,
"quake_keep": false,
"quake_lo": 1,
"retry": true,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 15,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v x * FStar.UInt64.v y / Prims.pow2 64 ==
FStar.UInt128.phh x y +
(FStar.UInt128.plh x y + FStar.UInt128.phl x y + FStar.UInt128.pll_h x y) / Prims.pow2 32) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"Prims.unit",
"FStar.Math.Lemmas.division_addition_lemma",
"Prims.op_Addition",
"FStar.UInt128.plh",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"Prims.pow2",
"FStar.UInt128.phh",
"FStar.UInt128.product_high32",
"FStar.UInt128.div_product",
"FStar.UInt128.mul_wide_high",
"FStar.Math.Lemmas.pow2_plus"
] | [] | true | false | true | false | false | let product_high_expand x y =
| Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
() | false |
FStar.UInt128.fst | FStar.UInt128.div_sum_combine1 | val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k)) | val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k)) | let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
() | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 4,
"end_line": 1152,
"start_col": 0,
"start_line": 1148
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures (n / k + m / k) * k == n - n % k + (m - m % k)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"Prims.unit",
"FStar.UInt128.div_mod",
"FStar.Math.Lemmas.distributivity_add_left",
"Prims.op_Division"
] | [] | true | false | true | false | false | let div_sum_combine1 n m k =
| Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
() | false |
FStar.UInt128.fst | FStar.UInt128.sum_rounded_mod_exact | val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k)) | val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k)) | let sum_rounded_mod_exact n m k =
n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n%k) (m - m%k) k;
Math.div_exact_r ((n - n%k) + (m - m % k)) k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 46,
"end_line": 1174,
"start_col": 0,
"start_line": 1168
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = ()
let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k
let sub_mod_gt_0 (n:nat) (k:pos) :
Lemma (0 <= n - n % k) = ()
val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 5,
"quake_keep": false,
"quake_lo": 1,
"retry": true,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures ((n - n % k + (m - m % k)) / k) * k == n - n % k + (m - m % k)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.div_exact_r",
"Prims.op_Addition",
"Prims.op_Subtraction",
"Prims.op_Modulus",
"Prims.unit",
"FStar.UInt128.mod_add",
"FStar.UInt128.sub_mod_gt_0",
"FStar.UInt128.n_minus_mod_exact"
] | [] | true | false | true | false | false | let sum_rounded_mod_exact n m k =
| n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n % k) (m - m % k) k;
Math.div_exact_r ((n - n % k) + (m - m % k)) k | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide_high_ok | val mul_wide_high_ok (x y: U64.t) : Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) | val mul_wide_high_ok (x y: U64.t) : Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) | let mul_wide_high_ok (x y: U64.t) :
Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) =
product_high_expand x y;
sum_shift_carry (phl x y + pll_h x y) (plh x y) (pow2 32) | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 59,
"end_line": 1199,
"start_col": 0,
"start_line": 1196
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = ()
let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k
let sub_mod_gt_0 (n:nat) (k:pos) :
Lemma (0 <= n - n % k) = ()
val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k))
#push-options "--retry 5" // sporadically fails
let sum_rounded_mod_exact n m k =
n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n%k) (m - m%k) k;
Math.div_exact_r ((n - n%k) + (m - m % k)) k
#pop-options
val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k)
#push-options "--retry 5" // sporadically fails
let div_sum_combine n m k =
sum_rounded_mod_exact n m k;
div_sum_combine1 n m k;
mul_injective (n / k + m / k) (((n - n%k) + (m - m%k)) / k) k;
assert (n + m - n % k - m % k == (n - n%k) + (m - m%k))
#pop-options
val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k)
let sum_shift_carry a b k =
div_sum_combine a (b+a%k) k;
// assert (a / k + (b + a%k) / k == (a + b + (a % k - a % k) - (b + a%k) % k) / k);
// assert ((a + b + (a % k - a % k) - (b + a%k) % k) / k == (a + b - (b + a%k) % k) / k);
add_mod_then_mod b a k;
Math.lemma_mod_spec (a+b) k | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt64.v x * FStar.UInt64.v y / Prims.pow2 64 == FStar.UInt128.mul_wide_high x y) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.sum_shift_carry",
"Prims.op_Addition",
"FStar.UInt128.phl",
"FStar.UInt128.pll_h",
"FStar.UInt128.plh",
"Prims.pow2",
"Prims.unit",
"FStar.UInt128.product_high_expand",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Division",
"FStar.Mul.op_Star",
"FStar.UInt64.v",
"FStar.UInt128.mul_wide_high",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let mul_wide_high_ok (x y: U64.t) : Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) =
| product_high_expand x y;
sum_shift_carry (phl x y + pll_h x y) (plh x y) (pow2 32) | false |
FStar.UInt128.fst | FStar.UInt128.n_minus_mod_exact | val n_minus_mod_exact (n: nat) (k: pos) : Lemma ((n - n % k) % k == 0) | val n_minus_mod_exact (n: nat) (k: pos) : Lemma ((n - n % k) % k == 0) | let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 11,
"end_line": 1160,
"start_col": 0,
"start_line": 1157
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (ensures (n - n % k) % k == 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.pos",
"FStar.UInt128.mod_0",
"Prims.unit",
"FStar.UInt128.mod_spec_mod",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Subtraction",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let n_minus_mod_exact (n: nat) (k: pos) : Lemma ((n - n % k) % k == 0) =
| mod_spec_mod n k;
mod_0 k | false |
FStar.UInt128.fst | FStar.UInt128.product_div_bound | val product_div_bound (#n: pos) (x y: UInt.uint_t n) : Lemma (x * y / pow2 n < pow2 n) | val product_div_bound (#n: pos) (x y: UInt.uint_t n) : Lemma (x * y / pow2 n < pow2 n) | let product_div_bound (#n:pos) (x y: UInt.uint_t n) :
Lemma (x * y / pow2 n < pow2 n) =
Math.pow2_plus n n;
product_bound x y (pow2 n);
pow2_div_bound #(n+n) (x * y) n | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 1205,
"start_col": 0,
"start_line": 1201
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = ()
let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k
let sub_mod_gt_0 (n:nat) (k:pos) :
Lemma (0 <= n - n % k) = ()
val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k))
#push-options "--retry 5" // sporadically fails
let sum_rounded_mod_exact n m k =
n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n%k) (m - m%k) k;
Math.div_exact_r ((n - n%k) + (m - m % k)) k
#pop-options
val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k)
#push-options "--retry 5" // sporadically fails
let div_sum_combine n m k =
sum_rounded_mod_exact n m k;
div_sum_combine1 n m k;
mul_injective (n / k + m / k) (((n - n%k) + (m - m%k)) / k) k;
assert (n + m - n % k - m % k == (n - n%k) + (m - m%k))
#pop-options
val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k)
let sum_shift_carry a b k =
div_sum_combine a (b+a%k) k;
// assert (a / k + (b + a%k) / k == (a + b + (a % k - a % k) - (b + a%k) % k) / k);
// assert ((a + b + (a % k - a % k) - (b + a%k) % k) / k == (a + b - (b + a%k) % k) / k);
add_mod_then_mod b a k;
Math.lemma_mod_spec (a+b) k
let mul_wide_high_ok (x y: U64.t) :
Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) =
product_high_expand x y;
sum_shift_carry (phl x y + pll_h x y) (plh x y) (pow2 32) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt.uint_t n -> y: FStar.UInt.uint_t n
-> FStar.Pervasives.Lemma (ensures x * y / Prims.pow2 n < Prims.pow2 n) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.pos",
"FStar.UInt.uint_t",
"FStar.UInt128.pow2_div_bound",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.unit",
"FStar.UInt128.product_bound",
"Prims.pow2",
"FStar.Math.Lemmas.pow2_plus",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Division",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let product_div_bound (#n: pos) (x y: UInt.uint_t n) : Lemma (x * y / pow2 n < pow2 n) =
| Math.pow2_plus n n;
product_bound x y (pow2 n);
pow2_div_bound #(n + n) (x * y) n | false |
FStar.UInt128.fst | FStar.UInt128.mul_wide | val mul_wide: x:U64.t -> y:U64.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U64.v y)) | val mul_wide: x:U64.t -> y:U64.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U64.v y)) | let mul_wide (x y:U64.t) : Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U64.v y)) =
mul_wide_low_ok x y;
mul_wide_high_ok x y;
product_div_bound (U64.v x) (U64.v y);
Math.modulo_lemma (mul_wide_high x y) (pow2 64);
mul_wide_impl x y | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 19,
"end_line": 1214,
"start_col": 0,
"start_line": 1207
} | (*
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 FStar.UInt128
open FStar.Mul
module UInt = FStar.UInt
module Seq = FStar.Seq
module BV = FStar.BitVector
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module Math = FStar.Math.Lemmas
open FStar.BV
open FStar.Tactics.V2
module T = FStar.Tactics.V2
module TBV = FStar.Tactics.BV
#set-options "--max_fuel 0 --max_ifuel 0 --split_queries no"
#set-options "--using_facts_from '*,-FStar.Tactics,-FStar.Reflection'"
(* TODO: explain why exactly this is needed? It leads to failures in
HACL* otherwise, claiming that some functions are not Low*. *)
#set-options "--normalize_pure_terms_for_extraction"
[@@ noextract_to "krml"]
noextract
let carry_uint64 (a b: uint_t 64) : Tot (uint_t 64) =
let ( ^^ ) = UInt.logxor in
let ( |^ ) = UInt.logor in
let ( -%^ ) = UInt.sub_mod in
let ( >>^ ) = UInt.shift_right in
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63
[@@ noextract_to "krml"]
noextract
let carry_bv (a b: uint_t 64) =
bvshr (bvxor (int2bv a)
(bvor (bvxor (int2bv a) (int2bv b)) (bvxor (bvsub (int2bv a) (int2bv b)) (int2bv b))))
63
let carry_uint64_ok (a b:uint_t 64)
: squash (int2bv (carry_uint64 a b) == carry_bv a b)
= _ by (T.norm [delta_only [`%carry_uint64]; unascribe];
let open FStar.Tactics.BV in
mapply (`trans);
arith_to_bv_tac ();
arith_to_bv_tac ();
T.norm [delta_only [`%carry_bv]];
trefl())
let fact1 (a b: uint_t 64) = carry_bv a b == int2bv 1
let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0
let lem_ult_1 (a b: uint_t 64)
: squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
= assert (bvult (int2bv a) (int2bv b) ==> fact1 a b)
by (T.norm [delta_only [`%fact1;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'";
smt())
let lem_ult_2 (a b:uint_t 64)
: squash (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
= assert (not (bvult (int2bv a) (int2bv b)) ==> fact0 a b)
by (T.norm [delta_only [`%fact0;`%carry_bv]];
set_options "--smtencoding.elim_box true --using_facts_from '__Nothing__' --z3smtopt '(set-option :smt.case_split 1)'")
let int2bv_ult (#n: pos) (a b: uint_t n)
: Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
= introduce (a < b) ==> (bvult #n (int2bv #n a) (int2bv #n b))
with _ . FStar.BV.int2bv_lemma_ult_1 a b;
introduce (bvult #n (int2bv #n a) (int2bv #n b)) ==> (a < b)
with _ . FStar.BV.int2bv_lemma_ult_2 a b
let lem_ult (a b:uint_t 64)
: Lemma (if a < b
then fact1 a b
else fact0 a b)
= int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b
let constant_time_carry (a b: U64.t) : Tot U64.t =
let open U64 in
// CONSTANT_TIME_CARRY macro
// ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
// 63 = sizeof(a) * 8 - 1
a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul
let carry_uint64_equiv (a b:UInt64.t)
: Lemma (U64.v (constant_time_carry a b) == carry_uint64 (U64.v a) (U64.v b))
= ()
// This type gets a special treatment in KaRaMeL and its definition is never
// printed in the resulting C file.
type uint128: Type0 = { low: U64.t; high: U64.t }
let t = uint128
let _ = intro_ambient n
let _ = intro_ambient t
[@@ noextract_to "krml"]
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
let div_mod (x:nat) (k:nat{k > 0}) : Lemma (x / k * k + x % k == x) = ()
let uint_to_t x =
div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64));
high = U64.uint_to_t (x / (pow2 64)); }
let v_inj (x1 x2: t): Lemma (requires (v x1 == v x2))
(ensures x1 == x2) =
assert (uint_to_t (v x1) == uint_to_t (v x2));
assert (uint_to_t (v x1) == x1);
assert (uint_to_t (v x2) == x2);
()
(* A weird helper used below... seems like the native encoding of
bitvectors may be making these proofs much harder than they should be? *)
let bv2int_fun (#n:pos) (a b : bv_t n)
: Lemma (requires a == b)
(ensures bv2int a == bv2int b)
= ()
(* This proof is quite brittle. It has a bunch of annotations to get
decent verification performance. *)
let constant_time_carry_ok (a b:U64.t)
: Lemma (constant_time_carry a b ==
(if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0))
= calc (==) {
U64.v (constant_time_carry a b);
(==) { carry_uint64_equiv a b }
carry_uint64 (U64.v a) (U64.v b);
(==) { inverse_num_lemma (carry_uint64 (U64.v a) (U64.v b)) }
bv2int (int2bv (carry_uint64 (U64.v a) (U64.v b)));
(==) { carry_uint64_ok (U64.v a) (U64.v b);
bv2int_fun (int2bv (carry_uint64 (U64.v a) (U64.v b))) (carry_bv (U64.v a) (U64.v b));
()
}
bv2int (carry_bv (U64.v a) (U64.v b));
(==) {
lem_ult (U64.v a) (U64.v b);
bv2int_fun (carry_bv (U64.v a) (U64.v b)) (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
}
bv2int
(if U64.v a < U64.v b
then int2bv 1
else int2bv 0);
};
assert (
bv2int (if U64.v a < U64.v b then int2bv 1 else int2bv 0)
== U64.v (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)) by (T.norm []);
U64.v_inj (constant_time_carry a b) (if U64.lt a b then U64.uint_to_t 1 else U64.uint_to_t 0)
let carry (a b: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r == (if U64.v a < U64.v b then 1 else 0))) =
constant_time_carry_ok a b;
constant_time_carry a b
let carry_sum_ok (a b:U64.t) :
Lemma (U64.v (carry (U64.add_mod a b) b) == (U64.v a + U64.v b) / (pow2 64)) = ()
let add (a b: t) : Pure t
(requires (v a + v b < pow2 128))
(ensures (fun r -> v a + v b = v r)) =
let l = U64.add_mod a.low b.low in
carry_sum_ok a.low b.low;
{ low = l;
high = U64.add (U64.add a.high b.high) (carry l b.low); }
let add_underspec (a b: t) =
let l = U64.add_mod a.low b.low in
begin
if v a + v b < pow2 128
then carry_sum_ok a.low b.low
else ()
end;
{ low = l;
high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low); }
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k)
let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k)
let mod_spec (a:nat) (k:nat{k > 0}) :
Lemma (a % k == a - a / k * k) = ()
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2)
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k =
Math.cancel_mul_div n k
val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} ->
Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % k)
let mod_add n1 n2 k = Math.modulo_distributivity n1 n2 k
val mod_add_small: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma
(requires (n1 % k + n2 % k < k))
(ensures (n1 % k + n2 % k == (n1 + n2) % k))
let mod_add_small n1 n2 k =
mod_add n1 n2 k;
Math.small_modulo_lemma_1 (n1%k + n2%k) k
// This proof is pretty stable with the calc proof, but it can fail
// ~1% of the times, so add a retry.
#push-options "--split_queries no --z3rlimit 20 --retry 5"
let add_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a + v b) % pow2 128 = v r)) =
let l = U64.add_mod a.low b.low in
let r = { low = l;
high = U64.add_mod (U64.add_mod a.high b.high) (carry l b.low)} in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
let b_l = U64.v b.low in
let b_h = U64.v b.high in
carry_sum_ok a.low b.low;
Math.lemma_mod_plus_distr_l (a_h + b_h) ((a_l + b_l) / (pow2 64)) (pow2 64);
calc (==) {
U64.v r.high * pow2 64;
== {}
((a_h + b_h + (a_l + b_l) / (pow2 64)) % pow2 64) * pow2 64;
== { mod_mul (a_h + b_h + (a_l + b_l) / (pow2 64)) (pow2 64) (pow2 64) }
((a_h + b_h + (a_l + b_l) / (pow2 64)) * pow2 64) % (pow2 64 * pow2 64);
== {}
((a_h + b_h + (a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
== {}
(a_h * pow2 64 + b_h * pow2 64 + ((a_l + b_l)/(pow2 64)) * pow2 64)
% pow2 128;
};
assert (U64.v r.low == (U64.v r.low) % pow2 128);
mod_add_small (a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64))
((a_l + b_l) % (pow2 64))
(pow2 128);
assert (U64.v r.low + U64.v r.high * pow2 64 ==
(a_h * pow2 64 +
b_h * pow2 64 +
(a_l + b_l) / (pow2 64) * (pow2 64) + (a_l + b_l) % (pow2 64)) % pow2 128);
mod_spec_rew_n (a_l + b_l) (pow2 64);
assert (v r ==
(a_h * pow2 64 +
b_h * pow2 64 +
a_l + b_l) % pow2 128);
assert_spinoff ((v a + v b) % pow2 128 = v r);
r
#pop-options
#push-options "--retry 5"
let sub (a b: t) : Pure t
(requires (v a - v b >= 0))
(ensures (fun r -> v r = v a - v b)) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub (U64.sub a.high b.high) (carry a.low l); }
#pop-options
let sub_underspec (a b: t) =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_underspec (U64.sub_underspec a.high b.high) (carry a.low l); }
let sub_mod_impl (a b: t) : t =
let l = U64.sub_mod a.low b.low in
{ low = l;
high = U64.sub_mod (U64.sub_mod a.high b.high) (carry a.low l); }
#push-options "--retry 10" // flaky
let sub_mod_pos_ok (a b:t) : Lemma
(requires (v a - v b >= 0))
(ensures (v (sub_mod_impl a b) = v a - v b)) =
assert (sub a b == sub_mod_impl a b);
()
#pop-options
val u64_diff_wrap : a:U64.t -> b:U64.t ->
Lemma (requires (U64.v a < U64.v b))
(ensures (U64.v (U64.sub_mod a b) == U64.v a - U64.v b + pow2 64))
let u64_diff_wrap a b = ()
#push-options "--z3rlimit 20"
val sub_mod_wrap1_ok : a:t -> b:t -> Lemma
(requires (v a - v b < 0 /\ U64.v a.low < U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128))
#push-options "--retry 10"
let sub_mod_wrap1_ok a b =
// carry == 1 and subtraction in low wraps
let l = U64.sub_mod a.low b.low in
assert (U64.v (carry a.low l) == 1);
u64_diff_wrap a.low b.low;
// a.high <= b.high since v a < v b;
// case split on equality and strictly less
if U64.v a.high = U64.v b.high then ()
else begin
u64_diff_wrap a.high b.high;
()
end
#pop-options
let sum_lt (a1 a2 b1 b2:nat) : Lemma
(requires (a1 + a2 < b1 + b2 /\ a1 >= b1))
(ensures (a2 < b2)) = ()
let sub_mod_wrap2_ok (a b:t) : Lemma
(requires (v a - v b < 0 /\ U64.v a.low >= U64.v b.low))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
// carry == 0, subtraction in low is exact, but subtraction in high
// must wrap since v a < v b
let l = U64.sub_mod a.low b.low in
let r = sub_mod_impl a b in
assert (U64.v l == U64.v a.low - U64.v b.low);
assert (U64.v (carry a.low l) == 0);
sum_lt (U64.v a.low) (U64.v a.high * pow2 64) (U64.v b.low) (U64.v b.high * pow2 64);
assert (U64.v (U64.sub_mod a.high b.high) == U64.v a.high - U64.v b.high + pow2 64);
()
let sub_mod_wrap_ok (a b:t) : Lemma
(requires (v a - v b < 0))
(ensures (v (sub_mod_impl a b) = v a - v b + pow2 128)) =
if U64.v a.low < U64.v b.low
then sub_mod_wrap1_ok a b
else sub_mod_wrap2_ok a b
#push-options "--z3rlimit 40"
let sub_mod (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = (v a - v b) % pow2 128)) =
(if v a - v b >= 0
then sub_mod_pos_ok a b
else sub_mod_wrap_ok a b);
sub_mod_impl a b
#pop-options
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n')
let shift_bound #n num n' =
Math.lemma_mult_le_right (pow2 n') num (pow2 n - 1);
Math.distributivity_sub_left (pow2 n) 1 (pow2 n');
Math.pow2_plus n' n
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1
val to_vec_append : #n1:nat{n1 > 0} -> #n2:nat{n2 > 0} -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 ->
Lemma (UInt.to_vec (append_uint num1 num2) == Seq.append (UInt.to_vec num2) (UInt.to_vec num1))
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
let to_vec_v (a: t) :
Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) =
to_vec_append (U64.v a.low) (U64.v a.high)
val logand_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2) ==
BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logand_vec a1 b1) (BV.logand_vec a2 b2))
(BV.logand_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logand (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) =
let r = { low = U64.logand a.low b.low;
high = U64.logand a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logand_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logand_vec (vec128 a) (vec128 b));
r
val logxor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2) ==
BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logxor_vec a1 b1) (BV.logxor_vec a2 b2))
(BV.logxor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logxor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logxor #128 (v a) (v b))) =
let r = { low = U64.logxor a.low b.low;
high = U64.logxor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logxor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logxor_vec (vec128 a) (vec128 b));
r
val logor_vec_append (#n1 #n2: pos) (a1 b1: BV.bv_t n1) (a2 b2: BV.bv_t n2) :
Lemma (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2) ==
BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor_vec_append #n1 #n2 a1 b1 a2 b2 =
Seq.lemma_eq_intro (Seq.append (BV.logor_vec a1 b1) (BV.logor_vec a2 b2))
(BV.logor_vec #(n1 + n2) (Seq.append a1 a2) (Seq.append b1 b2))
let logor (a b: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) =
let r = { low = U64.logor a.low b.low;
high = U64.logor a.high b.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
logor_vec_append (vec64 a.high) (vec64 b.high)
(vec64 a.low) (vec64 b.low);
to_vec_v a;
to_vec_v b;
assert (vec128 r == BV.logor_vec (vec128 a) (vec128 b));
r
val lognot_vec_append (#n1 #n2: pos) (a1: BV.bv_t n1) (a2: BV.bv_t n2) :
Lemma (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2) ==
BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot_vec_append #n1 #n2 a1 a2 =
Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2))
let lognot (a: t) : Pure t
(requires True)
(ensures (fun r -> v r = UInt.lognot #128 (v a))) =
let r = { low = U64.lognot a.low;
high = U64.lognot a.high } in
to_vec_v r;
assert (vec128 r == Seq.append (vec64 r.high) (vec64 r.low));
lognot_vec_append (vec64 a.high) (vec64 a.low);
to_vec_v a;
assert (vec128 r == BV.lognot_vec (vec128 a));
r
let mod_mul_cancel (n:nat) (k:nat{k > 0}) :
Lemma ((n * k) % k == 0) =
mod_spec (n * k) k;
mul_div_cancel n k;
()
let shift_past_mod (n:nat) (k1:nat) (k2:nat{k2 >= k1}) :
Lemma ((n * pow2 k2) % pow2 k1 == 0) =
assert (k2 == (k2 - k1) + k1);
Math.pow2_plus (k2 - k1) k1;
Math.paren_mul_right n (pow2 (k2 - k1)) (pow2 k1);
mod_mul_cancel (n * pow2 (k2 - k1)) (pow2 k1)
val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k =
mod_mod a k 1
let shift_left_large_val (#n1:nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s:nat) :
Lemma ((a1 + a2 * pow2 n1) * pow2 s == (a1 * pow2 s + a2 * pow2 (n1+s))) =
Math.distributivity_add_left a1 (a2 * pow2 n1) (pow2 s);
Math.paren_mul_right a2 (pow2 n1) (pow2 s);
Math.pow2_plus n1 s
#push-options "--z3rlimit 40"
let shift_left_large_lemma (#n1: nat) (#n2: nat) (a1:UInt.uint_t n1) (a2:UInt.uint_t n2) (s: nat{s >= n2}) :
Lemma (((a1 + a2 * pow2 n1) * pow2 s) % pow2 (n1+n2) ==
(a1 * pow2 s) % pow2 (n1+n2)) =
shift_left_large_val a1 a2 s;
mod_add (a1 * pow2 s) (a2 * pow2 (n1+s)) (pow2 (n1+n2));
shift_past_mod a2 (n1+n2) (n1+s);
mod_double (a1 * pow2 s) (pow2 (n1+n2));
()
#pop-options
val shift_left_large_lemma_t : a:t -> s:nat ->
Lemma (requires (s >= 64))
(ensures ((v a * pow2 s) % pow2 128 ==
(U64.v a.low * pow2 s) % pow2 128))
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
private let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
val div_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma
(requires True)
(ensures (a / pow2 (n1 - n2) == a * pow2 n2 / pow2 n1))
let div_pow2_diff a n1 n2 =
Math.pow2_plus n2 (n1-n2);
assert (a * pow2 n2 / pow2 n1 == a * pow2 n2 / (pow2 n2 * pow2 (n1 - n2)));
div_product (a * pow2 n2) (pow2 n2) (pow2 (n1-n2));
mul_div_cancel a (pow2 n2)
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat ->
Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
let mod_mul_pow2 n e1 e2 =
Math.lemma_mod_lt n (pow2 e1);
Math.lemma_mult_le_right (pow2 e2) (n % pow2 e1) (pow2 e1 - 1);
assert (n % pow2 e1 * pow2 e2 <= pow2 e1 * pow2 e2 - pow2 e2);
Math.pow2_plus e1 e2
let pow2_div_bound #b (n:UInt.uint_t b) (s:nat{s <= b}) :
Lemma (n / pow2 s < pow2 (b - s)) =
Math.lemma_div_lt n b s
#push-options "--smtencoding.l_arith_repr native --z3rlimit 40"
let add_u64_shift_left (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r = (U64.v hi * pow2 (U32.v s)) % pow2 64 + U64.v lo / pow2 (64 - U32.v s))) =
let high = U64.shift_left hi s in
let low = U64.shift_right lo (U32.sub u32_64 s) in
let s = U32.v s in
let high_n = U64.v hi % pow2 (64 - s) * pow2 s in
let low_n = U64.v lo / pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 s) (pow2 (64-s));
assert (U64.v high == high_n);
assert (U64.v low == low_n);
pow2_div_bound (U64.v lo) (64-s);
assert (low_n < pow2 s);
mod_mul_pow2 (U64.v hi) (64 - s) s;
U64.add high low
#pop-options
let div_plus_multiple (a:nat) (b:nat) (k:pos) :
Lemma (requires (a < k))
(ensures ((a + b * k) / k == b)) =
Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k
val div_add_small: n:nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (k1*m / (k1*k2) == (n + k1*m) / (k1*k2)))
let div_add_small n m k1 k2 =
div_product (k1*m) k1 k2;
div_product (n+k1*m) k1 k2;
mul_div_cancel m k1;
assert (k1*m/k1 == m);
div_plus_multiple n m k1
val add_mod_small: n: nat -> m:nat -> k1:pos -> k2:pos ->
Lemma (requires (n < k1))
(ensures (n + (k1 * m) % (k1 * k2) ==
(n + k1 * m) % (k1 * k2)))
let add_mod_small n m k1 k2 =
mod_spec (k1 * m) (k1 * k2);
mod_spec (n + k1 * m) (k1 * k2);
div_add_small n m k1 k2
let mod_then_mul_64 (n:nat) : Lemma (n % pow2 64 * pow2 64 == n * pow2 64 % pow2 128) =
Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64)
let mul_abc_to_acb (a b c: int) : Lemma (a * b * c == a * c * b) = ()
let add_u64_shift_left_respec (hi lo:U64.t) (s:U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r ->
U64.v r * pow2 64 ==
(U64.v hi * pow2 64) * pow2 (U32.v s) % pow2 128 +
U64.v lo * pow2 (U32.v s) / pow2 64 * pow2 64)) =
let r = add_u64_shift_left hi lo s in
let hi = U64.v hi in
let lo = U64.v lo in
let s = U32.v s in
// spec of add_u64_shift_left
assert (U64.v r == hi * pow2 s % pow2 64 + lo / pow2 (64 - s));
Math.distributivity_add_left (hi * pow2 s % pow2 64) (lo / pow2 (64-s)) (pow2 64);
mod_then_mul_64 (hi * pow2 s);
assert (hi * pow2 s % pow2 64 * pow2 64 == (hi * pow2 s * pow2 64) % pow2 128);
div_pow2_diff lo 64 s;
assert (lo / pow2 (64-s) == lo * pow2 s / pow2 64);
assert (U64.v r * pow2 64 == hi * pow2 s * pow2 64 % pow2 128 + lo * pow2 s / pow2 64 * pow2 64);
mul_abc_to_acb hi (pow2 s) (pow2 64);
r
val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k))
let add_mod_small' n m k =
Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k
#push-options "--retry 5"
let shift_t_val (a: t) (s: nat) :
Lemma (v a * pow2 s == U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s)) =
Math.pow2_plus 64 s;
()
#pop-options
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
#push-options "--retry 5"
let mul_mod_bound n s1 s2 =
// n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1
// n % pow2 (s2-s1) <= pow2 (s2-s1) - 1
// n % pow2 (s2-s1) * pow2 s1 <= pow2 s2 - pow2 s1
mod_mul n (pow2 s1) (pow2 (s2-s1));
// assert (n * pow2 s1 % pow2 s2 == n % pow2 (s2-s1) * pow2 s1);
Math.lemma_mod_lt n (pow2 (s2-s1));
Math.lemma_mult_le_right (pow2 s1) (n % pow2 (s2-s1)) (pow2 (s2-s1) - 1);
Math.pow2_plus (s2-s1) s1
#pop-options
let add_lt_le (a a' b b': int) :
Lemma (requires (a < a' /\ b <= b'))
(ensures (a + b < a' + b')) = ()
let u64_pow2_bound (a: UInt.uint_t 64) (s: nat) :
Lemma (a * pow2 s < pow2 (64+s)) =
Math.pow2_plus 64 s;
Math.lemma_mult_le_right (pow2 s) a (pow2 64)
let shift_t_mod_val' (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + U64.v a.high * pow2 (64+s) % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
u64_pow2_bound a_l s;
mul_mod_bound a_h (64+s) 128;
// assert (a_h * pow2 (64+s) % pow2 128 <= pow2 128 - pow2 (64+s));
add_lt_le (a_l * pow2 s) (pow2 (64+s)) (a_h * pow2 (64+s) % pow2 128) (pow2 128 - pow2 (64+s));
add_mod_small' (a_l * pow2 s) (a_h * pow2 (64+s)) (pow2 128);
shift_t_val a s;
()
let shift_t_mod_val (a: t) (s: nat{s < 64}) :
Lemma ((v a * pow2 s) % pow2 128 ==
U64.v a.low * pow2 s + (U64.v a.high * pow2 64) * pow2 s % pow2 128) =
let a_l = U64.v a.low in
let a_h = U64.v a.high in
shift_t_mod_val' a s;
Math.pow2_plus 64 s;
Math.paren_mul_right a_h (pow2 64) (pow2 s);
()
#push-options "--z3rlimit 20"
let shift_left_small (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 64))
(ensures (fun r -> v r = (v a * pow2 (U32.v s)) % pow2 128)) =
if U32.eq s 0ul then a
else
let r = { low = U64.shift_left a.low s;
high = add_u64_shift_left_respec a.high a.low s; } in
let s = U32.v s in
let a_l = U64.v a.low in
let a_h = U64.v a.high in
mod_spec_rew_n (a_l * pow2 s) (pow2 64);
shift_t_mod_val a s;
r
#pop-options
val shift_left_large : a:t -> s:U32.t{U32.v s >= 64 /\ U32.v s < 128} ->
r:t{v r = (v a * pow2 (U32.v s)) % pow2 128}
#push-options "--z3rlimit 50 --retry 5" // sporadically fails
let shift_left_large a s =
let h_shift = U32.sub s u32_64 in
assert (U32.v h_shift < 64);
let r = { low = U64.uint_to_t 0;
high = U64.shift_left a.low h_shift; } in
assert (U64.v r.high == (U64.v a.low * pow2 (U32.v s - 64)) % pow2 64);
mod_mul (U64.v a.low * pow2 (U32.v s - 64)) (pow2 64) (pow2 64);
Math.pow2_plus (U32.v s - 64) 64;
assert (U64.v r.high * pow2 64 == (U64.v a.low * pow2 (U32.v s)) % pow2 128);
shift_left_large_lemma_t a (U32.v s);
r
#pop-options
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s
else shift_left_large a s
let add_u64_shift_right (hi lo: U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 (64 - U32.v s) % pow2 64)) =
let low = U64.shift_right lo s in
let high = U64.shift_left hi (U32.sub u32_64 s) in
let s = U32.v s in
let low_n = U64.v lo / pow2 s in
let high_n = U64.v hi % pow2 s * pow2 (64 - s) in
Math.pow2_plus (64-s) s;
mod_mul (U64.v hi) (pow2 (64-s)) (pow2 s);
assert (U64.v high == high_n);
pow2_div_bound (U64.v lo) s;
assert (low_n < pow2 (64 - s));
mod_mul_pow2 (U64.v hi) s (64 - s);
U64.add low high
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2)
let mul_pow2_diff a n1 n2 =
Math.paren_mul_right a (pow2 (n1-n2)) (pow2 n2);
mul_div_cancel (a * pow2 (n1 - n2)) (pow2 n2);
Math.pow2_plus (n1 - n2) n2
let add_u64_shift_right_respec (hi lo:U64.t) (s: U32.t{U32.v s < 64}) : Pure U64.t
(requires (U32.v s <> 0))
(ensures (fun r -> U64.v r == U64.v lo / pow2 (U32.v s) +
U64.v hi * pow2 64 / pow2 (U32.v s) % pow2 64)) =
let r = add_u64_shift_right hi lo s in
let s = U32.v s in
mul_pow2_diff (U64.v hi) 64 s;
r
let mul_div_spec (n:nat) (k:pos) : Lemma (n / k * k == n - n % k) = ()
let mul_distr_sub (n1 n2:nat) (k:nat) : Lemma ((n1 - n2) * k == n1 * k - n2 * k) = ()
val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1
val shift_right_reconstruct : a_h:UInt.uint_t 64 -> s:nat{s < 64} ->
Lemma (a_h * pow2 (64-s) == a_h / pow2 s * pow2 64 + a_h * pow2 64 / pow2 s % pow2 64)
let shift_right_reconstruct a_h s =
mul_pow2_diff a_h 64 s;
mod_spec_rew_n (a_h * pow2 (64-s)) (pow2 64);
div_product_comm (a_h * pow2 64) (pow2 s) (pow2 64);
mul_div_cancel a_h (pow2 64);
assert (a_h / pow2 s * pow2 64 == a_h * pow2 64 / pow2 s / pow2 64 * pow2 64);
()
val u128_div_pow2 (a: t) (s:nat{s < 64}) :
Lemma (v a / pow2 s == U64.v a.low / pow2 s + U64.v a.high * pow2 (64 - s))
let u128_div_pow2 a s =
Math.pow2_plus (64-s) s;
Math.paren_mul_right (U64.v a.high) (pow2 (64-s)) (pow2 s);
Math.division_addition_lemma (U64.v a.low) (pow2 s) (U64.v a.high * pow2 (64 - s))
let shift_right_small (a: t) (s: U32.t{U32.v s < 64}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.eq s 0ul then a
else
let r = { low = add_u64_shift_right_respec a.high a.low s;
high = U64.shift_right a.high s; } in
let a_h = U64.v a.high in
let a_l = U64.v a.low in
let s = U32.v s in
shift_right_reconstruct a_h s;
assert (v r == a_h * pow2 (64-s) + a_l / pow2 s);
u128_div_pow2 a s;
r
let shift_right_large (a: t) (s: U32.t{U32.v s >= 64 /\ U32.v s < 128}) : Pure t
(requires True)
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
let r = { high = U64.uint_to_t 0;
low = U64.shift_right a.high (U32.sub s u32_64); } in
let s = U32.v s in
Math.pow2_plus 64 (s - 64);
div_product (v a) (pow2 64) (pow2 (s - 64));
assert (v a / pow2 s == v a / pow2 64 / pow2 (s - 64));
div_plus_multiple (U64.v a.low) (U64.v a.high) (pow2 64);
r
let shift_right (a: t) (s: U32.t) : Pure t
(requires (U32.v s < 128))
(ensures (fun r -> v r == v a / pow2 (U32.v s))) =
if U32.lt s u32_64
then shift_right_small a s
else shift_right_large a s
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low)
let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low)
let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low)
let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low)
let u64_logand_comm (a b:U64.t) : Lemma (U64.logand a b == U64.logand b a) =
UInt.logand_commutative (U64.v a) (U64.v b)
val u64_and_0 (a b:U64.t) :
Lemma (U64.v b = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)]
let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a)
let u64_0_and (a b:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0)
[SMTPat (U64.logand a b)] =
u64_logand_comm a b
val u64_1s_and (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 /\
U64.v b = pow2 64 - 1 ==> U64.v (U64.logand a b) = pow2 64 - 1)
[SMTPat (U64.logand a b)]
let u64_1s_and a b = UInt.logand_lemma_2 (U64.v a)
let eq_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a = v b ==> v r = pow2 128 - 1) /\ (v a <> v b ==> v r = 0))) =
let mask = U64.logand (U64.eq_mask a.low b.low)
(U64.eq_mask a.high b.high) in
{ low = mask; high = mask; }
private let gte_characterization (a b: t) :
Lemma (v a >= v b ==>
U64.v a.high > U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low >= U64.v b.low)) = ()
private let lt_characterization (a b: t) :
Lemma (v a < v b ==>
U64.v a.high < U64.v b.high \/
(U64.v a.high = U64.v b.high /\ U64.v a.low < U64.v b.low)) = ()
let u64_logor_comm (a b:U64.t) : Lemma (U64.logor a b == U64.logor b a) =
UInt.logor_commutative (U64.v a) (U64.v b)
val u64_or_1 (a b:U64.t) :
Lemma (U64.v b = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)]
let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a)
let u64_1_or (a b:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.logor a b) = pow2 64 - 1)
[SMTPat (U64.logor a b)] =
u64_logor_comm a b
val u64_or_0 (a b:U64.t) :
Lemma (U64.v a = 0 /\ U64.v b = 0 ==> U64.v (U64.logor a b) = 0)
[SMTPat (U64.logor a b)]
let u64_or_0 a b = UInt.logor_lemma_1 (U64.v a)
val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)]
let u64_not_0 a = UInt.lognot_lemma_1 #64
val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)]
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
let gte_mask (a b: t) : Pure t
(requires True)
(ensures (fun r -> (v a >= v b ==> v r = pow2 128 - 1) /\ (v a < v b ==> v r = 0))) =
let mask_hi_gte = U64.logand (U64.gte_mask a.high b.high)
(U64.lognot (U64.eq_mask a.high b.high)) in
let mask_lo_gte = U64.logand (U64.eq_mask a.high b.high)
(U64.gte_mask a.low b.low) in
let mask = U64.logor mask_hi_gte mask_lo_gte in
gte_characterization a b;
lt_characterization a b;
{ low = mask; high = mask; }
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
inline_for_extraction
let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff
let u64_mod_32 (a: U64.t) : Pure U64.t
(requires True)
(ensures (fun r -> U64.v r = U64.v a % pow2 32)) =
UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask
let u64_32_digits (a: U64.t) : Lemma (U64.v a / pow2 32 * pow2 32 + U64.v a % pow2 32 == U64.v a) =
div_mod (U64.v a) (pow2 32)
val mul32_digits : x:UInt.uint_t 64 -> y:UInt.uint_t 32 ->
Lemma (x * y == (x / pow2 32 * y) * pow2 32 + (x % pow2 32) * y)
let mul32_digits x y = ()
let u32_32 : x:U32.t{U32.v x == 32} = U32.uint_to_t 32
#push-options "--z3rlimit 30"
let u32_combine (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi % pow2 32 * pow2 32 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
#pop-options
let product_bound (a b:nat) (k:pos) :
Lemma (requires (a < k /\ b < k))
(ensures a * b <= k * k - 2*k + 1) =
Math.lemma_mult_le_right b a (k-1);
Math.lemma_mult_le_left (k-1) b (k-1)
val uint_product_bound : #n:nat -> a:UInt.uint_t n -> b:UInt.uint_t n ->
Lemma (a * b <= pow2 (2*n) - 2*(pow2 n) + 1)
let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n
val u32_product_bound : a:nat{a < pow2 32} -> b:nat{b < pow2 32} ->
Lemma (UInt.size (a * b) 64 /\ a * b < pow2 64 - pow2 32 - 1)
let u32_product_bound a b =
uint_product_bound #32 a b
let mul32 x y =
let x0 = u64_mod_32 x in
let x1 = U64.shift_right x u32_32 in
u32_product_bound (U64.v x0) (U32.v y);
let x0y = U64.mul x0 (FStar.Int.Cast.uint32_to_uint64 y) in
let x0yl = u64_mod_32 x0y in
let x0yh = U64.shift_right x0y u32_32 in
u32_product_bound (U64.v x1) (U32.v y);
// not in the original C code
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
// correspondence with C:
// r0 = r.low
// r0 is written using u32_combine hi lo = lo + hi << 32
// r1 = r.high
let r = { low = u32_combine x1y x0yl;
high = U64.shift_right x1y u32_32; } in
u64_32_digits x;
//assert (U64.v x == U64.v x1 * pow2 32 + U64.v x0);
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
//assert (U64.v x0y == U64.v x0yh * pow2 32 + U64.v x0yl);
assert (U64.v x1y' == U64.v x / pow2 32 * U32.v y);
mul32_digits (U64.v x) (U32.v y);
assert (U64.v x * U32.v y == U64.v x1y' * pow2 32 + U64.v x0y);
r
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
val mul32_bound : x:UInt.uint_t 32 -> y:UInt.uint_t 32 ->
n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1 /\ n == x * y}
let mul32_bound x y =
u32_product_bound x y;
x * y
let pll (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (l32 (U64.v y))
let plh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (l32 (U64.v x)) (h32 (U64.v y))
let phl (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (l32 (U64.v y))
let phh (x y: U64.t) : n:UInt.uint_t 64{n < pow2 64 - pow2 32 - 1} =
mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
let mul_wide_low (x y: U64.t) = (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y
let mul_wide_high (x y: U64.t) =
phh x y +
(phl x y + pll_h x y) / pow2 32 +
(plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32
inline_for_extraction noextract
let mul_wide_impl_t' (x y: U64.t) : Pure (tuple4 U64.t U64.t U64.t U64.t)
(requires True)
(ensures (fun r -> let (u1, w3, x', t') = r in
U64.v u1 == U64.v x % pow2 32 /\
U64.v w3 == pll_l x y /\
U64.v x' == h32 (U64.v x) /\
U64.v t' == phl x y + pll_h x y)) =
let u1 = u64_mod_32 x in
let v1 = u64_mod_32 y in
u32_product_bound (U64.v u1) (U64.v v1);
let t = U64.mul u1 v1 in
assert (U64.v t == pll x y);
let w3 = u64_mod_32 t in
assert (U64.v w3 == pll_l x y);
let k = U64.shift_right t u32_32 in
assert (U64.v k == pll_h x y);
let x' = U64.shift_right x u32_32 in
assert (U64.v x' == h32 (U64.v x));
u32_product_bound (U64.v x') (U64.v v1);
let t' = U64.add (U64.mul x' v1) k in
(u1, w3, x', t')
// similar to u32_combine, but use % 2^64 * 2^32
let u32_combine' (hi lo: U64.t) : Pure U64.t
(requires (U64.v lo < pow2 32))
(ensures (fun r -> U64.v r = U64.v hi * pow2 32 % pow2 64 + U64.v lo)) =
U64.add lo (U64.shift_left hi u32_32)
inline_for_extraction noextract
let mul_wide_impl (x: U64.t) (y: U64.t) :
Tot (r:t{U64.v r.low == mul_wide_low x y /\
U64.v r.high == mul_wide_high x y % pow2 64}) =
let (u1, w3, x', t') = mul_wide_impl_t' x y in
let k' = u64_mod_32 t' in
let w1 = U64.shift_right t' u32_32 in
assert (U64.v w1 == (phl x y + pll_h x y) / pow2 32);
let y' = U64.shift_right y u32_32 in
assert (U64.v y' == h32 (U64.v y));
u32_product_bound (U64.v u1) (U64.v y');
let t'' = U64.add (U64.mul u1 y') k' in
assert (U64.v t'' == plh x y + (phl x y + pll_h x y) % pow2 32);
let k'' = U64.shift_right t'' u32_32 in
assert (U64.v k'' == (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32);
u32_product_bound (U64.v x') (U64.v y');
mod_mul_pow2 (U64.v t'') 32 64;
let r0 = u32_combine' t'' w3 in
// let r0 = U64.add (U64.shift_left t'' u32_32) w3 in
assert (U64.v r0 == (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y);
let xy_w1 = U64.add (U64.mul x' y') w1 in
assert (U64.v xy_w1 == phh x y + (phl x y + pll_h x y) / pow2 32);
let r1 = U64.add_mod xy_w1 k'' in
assert (U64.v r1 == (phh x y + (phl x y + pll_h x y) / pow2 32 + (plh x y + (phl x y + pll_h x y) % pow2 32) / pow2 32) % pow2 64);
let r = { low = r0; high = r1; } in
r
let product_sums (a b c d:nat) :
Lemma ((a + b) * (c + d) == a * c + a * d + b * c + b * d) = ()
val u64_32_product (xl xh yl yh:UInt.uint_t 32) :
Lemma ((xl + xh * pow2 32) * (yl + yh * pow2 32) ==
xl * yl + (xl * yh) * pow2 32 + (xh * yl) * pow2 32 + (xh * yh) * pow2 64)
#push-options "--z3rlimit 25"
let u64_32_product xl xh yl yh =
assert (xh >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (pow2 32 >= 0); //flakiness; without this, can't prove that (xh * pow2 32) >= 0
assert (xh*pow2 32 >= 0);
product_sums xl (xh*pow2 32) yl (yh*pow2 32);
mul_abc_to_acb xh (pow2 32) yl;
assert (xl * (yh * pow2 32) == (xl * yh) * pow2 32);
Math.pow2_plus 32 32;
assert ((xh * pow2 32) * (yh * pow2 32) == (xh * yh) * pow2 64)
#pop-options
let product_expand (x y: U64.t) :
Lemma (U64.v x * U64.v y == phh x y * pow2 64 +
(plh x y + phl x y + pll_h x y) * pow2 32 +
pll_l x y) =
assert (U64.v x == l32 (U64.v x) + h32 (U64.v x) * pow2 32);
assert (U64.v y == l32 (U64.v y) + h32 (U64.v y) * pow2 32);
u64_32_product (l32 (U64.v x)) (h32 (U64.v x)) (l32 (U64.v y)) (h32 (U64.v y))
let product_low_expand (x y: U64.t) :
Lemma ((U64.v x * U64.v y) % pow2 64 ==
((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64) =
product_expand x y;
Math.lemma_mod_plus ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) (phh x y) (pow2 64)
let add_mod_then_mod (n m:nat) (k:pos) :
Lemma ((n + m % k) % k == (n + m) % k) =
mod_add n m k;
mod_add n (m % k) k;
mod_double m k
let shift_add (n:nat) (m:nat{m < pow2 32}) :
Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) =
add_mod_small' m (n*pow2 32) (pow2 64)
let mul_wide_low_ok (x y: U64.t) :
Lemma (mul_wide_low x y == (U64.v x * U64.v y) % pow2 64) =
Math.pow2_plus 32 32;
mod_mul (plh x y + (phl x y + pll_h x y) % pow2 32) (pow2 32) (pow2 32);
assert (mul_wide_low x y ==
(plh x y + (phl x y + pll_h x y) % pow2 32) % pow2 32 * pow2 32 + pll_l x y);
add_mod_then_mod (plh x y) (phl x y + pll_h x y) (pow2 32);
assert (mul_wide_low x y == (plh x y + phl x y + pll_h x y) % pow2 32 * pow2 32 + pll_l x y);
mod_mul (plh x y + phl x y + pll_h x y) (pow2 32) (pow2 32);
shift_add (plh x y + phl x y + pll_h x y) (pll_l x y);
assert (mul_wide_low x y == ((plh x y + phl x y + pll_h x y) * pow2 32 + pll_l x y) % pow2 64);
product_low_expand x y
val product_high32 : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 32 == phh x y * pow2 32 + plh x y + phl x y + pll_h x y)
#push-options "--z3rlimit 20"
let product_high32 x y =
Math.pow2_plus 32 32;
product_expand x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y * pow2 32);
mul_div_cancel (phh x y * pow2 32) (pow2 32);
mul_div_cancel (plh x y + phl x y + pll_h x y) (pow2 32);
Math.small_division_lemma_1 (pll_l x y) (pow2 32)
#pop-options
val product_high_expand : x:U64.t -> y:U64.t ->
Lemma ((U64.v x * U64.v y) / pow2 64 == phh x y + (plh x y + phl x y + pll_h x y) / pow2 32)
#push-options "--z3rlimit 15 --retry 5" // sporadically fails
let product_high_expand x y =
Math.pow2_plus 32 32;
div_product (mul_wide_high x y) (pow2 32) (pow2 32);
product_high32 x y;
Math.division_addition_lemma (plh x y + phl x y + pll_h x y) (pow2 32) (phh x y);
()
#pop-options
val mod_spec_multiply : n:nat -> k:pos ->
Lemma ((n - n%k) / k * k == n - n%k)
let mod_spec_multiply n k =
Math.lemma_mod_spec2 n k
val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k
let mul_injective (n m:nat) (k:pos) :
Lemma (requires (n * k == m * k))
(ensures (n == m)) = ()
val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k))
let div_sum_combine1 n m k =
Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
()
let mod_0 (k:pos) :
Lemma (0 % k == 0) = ()
let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k
let sub_mod_gt_0 (n:nat) (k:pos) :
Lemma (0 <= n - n % k) = ()
val sum_rounded_mod_exact : n:nat -> m:nat -> k:pos ->
Lemma (((n - n%k) + (m - m%k)) / k * k == (n - n%k) + (m - m%k))
#push-options "--retry 5" // sporadically fails
let sum_rounded_mod_exact n m k =
n_minus_mod_exact n k;
n_minus_mod_exact m k;
sub_mod_gt_0 n k;
sub_mod_gt_0 m k;
mod_add (n - n%k) (m - m%k) k;
Math.div_exact_r ((n - n%k) + (m - m % k)) k
#pop-options
val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k)
#push-options "--retry 5" // sporadically fails
let div_sum_combine n m k =
sum_rounded_mod_exact n m k;
div_sum_combine1 n m k;
mul_injective (n / k + m / k) (((n - n%k) + (m - m%k)) / k) k;
assert (n + m - n % k - m % k == (n - n%k) + (m - m%k))
#pop-options
val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k)
let sum_shift_carry a b k =
div_sum_combine a (b+a%k) k;
// assert (a / k + (b + a%k) / k == (a + b + (a % k - a % k) - (b + a%k) % k) / k);
// assert ((a + b + (a % k - a % k) - (b + a%k) % k) / k == (a + b - (b + a%k) % k) / k);
add_mod_then_mod b a k;
Math.lemma_mod_spec (a+b) k
let mul_wide_high_ok (x y: U64.t) :
Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) =
product_high_expand x y;
sum_shift_carry (phl x y + pll_h x y) (plh x y) (pow2 32)
let product_div_bound (#n:pos) (x y: UInt.uint_t n) :
Lemma (x * y / pow2 n < pow2 n) =
Math.pow2_plus n n;
product_bound x y (pow2 n);
pow2_div_bound #(n+n) (x * y) n | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt.fsti.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.Effect.fsti.checked",
"FStar.Tactics.BV.fst.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Int.Cast.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Calc.fsti.checked",
"FStar.BV.fsti.checked",
"FStar.BitVector.fst.checked"
],
"interface_file": true,
"source_file": "FStar.UInt128.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.BV",
"short_module": "TBV"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.BV",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Math.Lemmas",
"short_module": "Math"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"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
}
] | {
"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": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.Pure FStar.UInt128.t | Prims.Pure | [] | [] | [
"FStar.UInt64.t",
"FStar.UInt128.mul_wide_impl",
"Prims.unit",
"FStar.Math.Lemmas.modulo_lemma",
"FStar.UInt128.mul_wide_high",
"Prims.pow2",
"FStar.UInt128.product_div_bound",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt128.mul_wide_high_ok",
"FStar.UInt128.mul_wide_low_ok",
"FStar.UInt128.t",
"Prims.l_True",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.v",
"FStar.Mul.op_Star"
] | [] | false | false | false | false | false | let mul_wide (x y: U64.t) : Pure t (requires True) (ensures (fun r -> v r == U64.v x * U64.v y)) =
| mul_wide_low_ok x y;
mul_wide_high_ok x y;
product_div_bound (U64.v x) (U64.v y);
Math.modulo_lemma (mul_wide_high x y) (pow2 64);
mul_wide_impl x y | false |
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.felem_list | val felem_list : Type0 | let felem_list = x:list uint64{FL.length x == 4} | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 48,
"end_line": 19,
"start_col": 0,
"start_line": 19
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3] | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Lib.IntTypes.uint64",
"Prims.eq2",
"Prims.int",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | true | true | let felem_list =
| x: list uint64 {FL.length x == 4} | false |
|
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.create4 | val create4 (x0 x1 x2 x3: uint64) : list uint64 | val create4 (x0 x1 x2 x3: uint64) : list uint64 | let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3] | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 65,
"end_line": 16,
"start_col": 0,
"start_line": 16
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0" | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
x0: Lib.IntTypes.uint64 ->
x1: Lib.IntTypes.uint64 ->
x2: Lib.IntTypes.uint64 ->
x3: Lib.IntTypes.uint64
-> Prims.list Lib.IntTypes.uint64 | Prims.Tot | [
"total"
] | [] | [
"Lib.IntTypes.uint64",
"Prims.Cons",
"Prims.Nil",
"Prims.list"
] | [] | false | false | false | true | false | let create4 (x0 x1 x2 x3: uint64) : list uint64 =
| [x0; x1; x2; x3] | false |
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.point_list | val point_list : Type0 | let point_list = x:list uint64{FL.length x == 12} | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 49,
"end_line": 21,
"start_col": 0,
"start_line": 21
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3]
inline_for_extraction noextract
let felem_list = x:list uint64{FL.length x == 4} | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Lib.IntTypes.uint64",
"Prims.eq2",
"Prims.int",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | true | true | let point_list =
| x: list uint64 {FL.length x == 12} | false |
|
FStar.OrdSet.fsti | FStar.OrdSet.mem_of | val mem_of : s: FStar.OrdSet.ordset a f -> x: a -> Prims.bool | let mem_of #a #f (s:ordset a f) x = mem x s | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 45,
"start_col": 7,
"start_line": 45
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: FStar.OrdSet.ordset a f -> x: a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.mem",
"Prims.bool"
] | [] | false | false | false | false | false | let mem_of #a #f (s: ordset a f) x =
| mem x s | false |
|
FStar.OrdSet.fsti | FStar.OrdSet.strict_superset | val strict_superset (#a #f: _) (s1 s2: ordset a f) : Tot bool | val strict_superset (#a #f: _) (s1 s2: ordset a f) : Tot bool | let strict_superset #a #f (s1 s2: ordset a f) : Tot bool = strict_subset s2 s1 | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 78,
"end_line": 88,
"start_col": 0,
"start_line": 88
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool
(* currying-friendly version of mem, ready to be used as a lambda *)
unfold let mem_of #a #f (s:ordset a f) x = mem x s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
(*
liat is the reverse of tail, i.e. a list of all but the last element.
A shortcut to (fst (unsnoc s)), which as a word is composed
in a remarkably similar fashion.
*)
val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
})
val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
})
val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
})
val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l))
val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a)
val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f)
val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat
val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let superset #a #f (s1 s2: ordset a f) : Tot bool = subset s2 s1
val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f)
val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.strict_subset",
"Prims.bool"
] | [] | false | false | false | false | false | let strict_superset #a #f (s1: ordset a f) (s2: ordset a f) : Tot bool =
| strict_subset s2 s1 | false |
FStar.OrdSet.fsti | FStar.OrdSet.superset | val superset (#a #f: _) (s1 s2: ordset a f) : Tot bool | val superset (#a #f: _) (s1 s2: ordset a f) : Tot bool | let superset #a #f (s1 s2: ordset a f) : Tot bool = subset s2 s1 | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 64,
"end_line": 81,
"start_col": 0,
"start_line": 81
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool
(* currying-friendly version of mem, ready to be used as a lambda *)
unfold let mem_of #a #f (s:ordset a f) x = mem x s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
(*
liat is the reverse of tail, i.e. a list of all but the last element.
A shortcut to (fst (unsnoc s)), which as a word is composed
in a remarkably similar fashion.
*)
val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
})
val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
})
val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
})
val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l))
val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a)
val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f)
val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"FStar.OrdSet.subset",
"Prims.bool"
] | [] | false | false | false | false | false | let superset #a #f (s1: ordset a f) (s2: ordset a f) : Tot bool =
| subset s2 s1 | false |
FStar.OrdSet.fsti | FStar.OrdSet.equal | val equal (#a: eqtype) (#f: cmp a) (s1 s2: ordset a f) : Tot prop | val equal (#a: eqtype) (#f: cmp a) (s1 s2: ordset a f) : Tot prop | let equal (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Tot prop =
forall x. mem #_ #f x s1 = mem #_ #f x s2 | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 93,
"start_col": 0,
"start_line": 92
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool
(* currying-friendly version of mem, ready to be used as a lambda *)
unfold let mem_of #a #f (s:ordset a f) x = mem x s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
(*
liat is the reverse of tail, i.e. a list of all but the last element.
A shortcut to (fst (unsnoc s)), which as a word is composed
in a remarkably similar fashion.
*)
val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
})
val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
})
val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
})
val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l))
val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a)
val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f)
val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat
val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let superset #a #f (s1 s2: ordset a f) : Tot bool = subset s2 s1
val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f)
val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val strict_subset: #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let strict_superset #a #f (s1 s2: ordset a f) : Tot bool = strict_subset s2 s1
let disjoint #a #f (s1 s2 : ordset a f) : Tot bool = intersect s1 s2 = empty | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> Prims.prop | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.l_Forall",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.OrdSet.mem",
"Prims.prop"
] | [] | false | false | false | false | true | let equal (#a: eqtype) (#f: cmp a) (s1 s2: ordset a f) : Tot prop =
| forall x. mem #_ #f x s1 = mem #_ #f x s2 | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.notin | val notin (#a: eqtype) (x: a) (s: set a) : bool | val notin (#a: eqtype) (x: a) (s: set a) : bool | let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 15,
"end_line": 150,
"start_col": 0,
"start_line": 148
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> s: FStar.FiniteSet.Base.set a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.op_Negation",
"FStar.FiniteSet.Base.mem",
"Prims.bool"
] | [] | false | false | false | false | false | let notin (#a: eqtype) (x: a) (s: set a) : bool =
| not (mem x s) | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.empty_set_contains_no_elements_fact | val empty_set_contains_no_elements_fact : Prims.logical | let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a)) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 81,
"end_line": 166,
"start_col": 0,
"start_line": 165
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"Prims.b2t",
"Prims.op_Negation",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.emptyset"
] | [] | false | false | false | true | true | let empty_set_contains_no_elements_fact =
| forall (a: eqtype) (o: a). {:pattern mem o (emptyset)} not (mem o (emptyset #a)) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.remove | val remove (#a: eqtype) (x: a) (s: set a) : set a | val remove (#a: eqtype) (x: a) (s: set a) : set a | let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 146,
"start_col": 0,
"start_line": 144
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`: | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> s: FStar.FiniteSet.Base.set a -> FStar.FiniteSet.Base.set a | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"FStar.FiniteSet.Base.difference",
"FStar.FiniteSet.Base.singleton"
] | [] | false | false | false | false | false | let remove (#a: eqtype) (x: a) (s: set a) : set a =
| difference s (singleton x) | false |
FStar.OrdSet.fsti | FStar.OrdSet.sorted | val sorted (#a: eqtype) (f: cmp a) (l: list a) : Tot bool | val sorted (#a: eqtype) (f: cmp a) (l: list a) : Tot bool | let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl) | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 51,
"end_line": 29,
"start_col": 0,
"start_line": 25
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f} | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: FStar.OrdSet.cmp a -> l: Prims.list a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"Prims.list",
"Prims.op_AmpAmp",
"Prims.op_disEquality",
"FStar.OrdSet.sorted",
"Prims.Cons",
"Prims.bool"
] | [
"recursion"
] | false | false | false | false | false | let rec sorted (#a: eqtype) (f: cmp a) (l: list a) : Tot bool =
| match l with
| [] -> true
| x :: [] -> true
| x :: y :: tl -> f x y && x <> y && sorted f (y :: tl) | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.singleton_contains_argument_fact | val singleton_contains_argument_fact : Prims.logical | let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 70,
"end_line": 184,
"start_col": 0,
"start_line": 183
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.singleton"
] | [] | false | false | false | true | true | let singleton_contains_argument_fact =
| forall (a: eqtype) (r: a). {:pattern singleton r} mem r (singleton r) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.singleton_contains_fact | val singleton_contains_fact : Prims.logical | let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 97,
"end_line": 191,
"start_col": 0,
"start_line": 190
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"Prims.l_iff",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.singleton",
"Prims.eq2"
] | [] | false | false | false | true | true | let singleton_contains_fact =
| forall (a: eqtype) (r: a) (o: a). {:pattern mem o (singleton r)} mem o (singleton r) <==> r == o | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.singleton_cardinality_fact | val singleton_cardinality_fact : Prims.logical | let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 94,
"end_line": 198,
"start_col": 0,
"start_line": 197
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.FiniteSet.Base.cardinality",
"FStar.FiniteSet.Base.singleton"
] | [] | false | false | false | true | true | let singleton_cardinality_fact =
| forall (a: eqtype) (r: a). {:pattern cardinality (singleton r)} cardinality (singleton r) = 1 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.length_zero_fact | val length_zero_fact : Prims.logical | let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s)) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 52,
"end_line": 177,
"start_col": 0,
"start_line": 174
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x]))); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_and",
"Prims.l_iff",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.FiniteSet.Base.cardinality",
"Prims.eq2",
"FStar.FiniteSet.Base.emptyset",
"Prims.op_disEquality",
"Prims.l_Exists",
"FStar.FiniteSet.Base.mem"
] | [] | false | false | false | true | true | let length_zero_fact =
| forall (a: eqtype) (s: set a). {:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset) /\ (cardinality s <> 0 <==> (exists x. mem x s)) | false |
|
FStar.OrdSet.fsti | FStar.OrdSet.disjoint | val disjoint (#a #f: _) (s1 s2: ordset a f) : Tot bool | val disjoint (#a #f: _) (s1 s2: ordset a f) : Tot bool | let disjoint #a #f (s1 s2 : ordset a f) : Tot bool = intersect s1 s2 = empty | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 76,
"end_line": 90,
"start_col": 0,
"start_line": 90
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool
(* currying-friendly version of mem, ready to be used as a lambda *)
unfold let mem_of #a #f (s:ordset a f) x = mem x s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
(*
liat is the reverse of tail, i.e. a list of all but the last element.
A shortcut to (fst (unsnoc s)), which as a word is composed
in a remarkably similar fashion.
*)
val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
})
val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
})
val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
})
val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l))
val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a)
val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f)
val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat
val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let superset #a #f (s1 s2: ordset a f) : Tot bool = subset s2 s1
val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f)
val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val strict_subset: #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let strict_superset #a #f (s1 s2: ordset a f) : Tot bool = strict_subset s2 s1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s1: FStar.OrdSet.ordset a f -> s2: FStar.OrdSet.ordset a f -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"FStar.OrdSet.cmp",
"FStar.OrdSet.ordset",
"Prims.op_Equality",
"FStar.OrdSet.intersect",
"FStar.OrdSet.empty",
"Prims.bool"
] | [] | false | false | false | false | false | let disjoint #a #f (s1: ordset a f) (s2: ordset a f) : Tot bool =
| intersect s1 s2 = empty | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.list_nonrepeating | val list_nonrepeating (#a: eqtype) (xs: list a) : bool | val list_nonrepeating (#a: eqtype) (xs: list a) : bool | let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 59,
"end_line": 60,
"start_col": 0,
"start_line": 57
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`: | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | xs: Prims.list a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"Prims.list",
"Prims.op_AmpAmp",
"Prims.op_Negation",
"FStar.List.Tot.Base.mem",
"FStar.FiniteSet.Base.list_nonrepeating",
"Prims.bool"
] | [
"recursion"
] | false | false | false | false | false | let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
| match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_fact | val insert_fact : Prims.logical | let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 207,
"start_col": 0,
"start_line": 205
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.insert",
"Prims.l_or",
"Prims.eq2"
] | [] | false | false | false | true | true | let insert_fact =
| forall (a: eqtype) (s: set a) (x: a) (o: a). {:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_contains_argument_fact | val insert_contains_argument_fact : Prims.logical | let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 22,
"end_line": 216,
"start_col": 0,
"start_line": 214
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.insert"
] | [] | false | false | false | true | true | let insert_contains_argument_fact =
| forall (a: eqtype) (s: set a) (x: a). {:pattern insert x s} mem x (insert x s) | false |
|
Spec.Blake2.Alternative.fst | Spec.Blake2.Alternative.lemma_spec_equivalence_update | val lemma_spec_equivalence_update:
a:alg
-> kk:size_nat{kk <= max_key a}
-> k:lbytes kk
-> d:bytes{if kk = 0 then length d <= max_limb a else length d + (size_block a) <= max_limb a}
-> s:state a ->
Lemma (blake2_update a kk k d s `Seq.equal` blake2_update' a kk k d s) | val lemma_spec_equivalence_update:
a:alg
-> kk:size_nat{kk <= max_key a}
-> k:lbytes kk
-> d:bytes{if kk = 0 then length d <= max_limb a else length d + (size_block a) <= max_limb a}
-> s:state a ->
Lemma (blake2_update a kk k d s `Seq.equal` blake2_update' a kk k d s) | let lemma_spec_equivalence_update a kk k d s =
let ll = length d in
let key_block: bytes = if kk > 0 then blake2_key_block a kk k else Seq.empty in
if kk = 0 then begin
assert (key_block `Seq.equal` Seq.empty);
assert ((key_block `Seq.append` d) `Seq.equal` d);
()
end else if ll = 0 then
let (nb,rem) = split a (length (blake2_key_block a kk k)) in
// let s = repeati nb (blake2_update1 a prev (blake2_key_block a kk k)) s in
calc (Seq.equal) {
blake2_update a kk k d s;
(Seq.equal) {}
blake2_update_key a kk k ll s;
(Seq.equal) {}
blake2_update_block a true (size_block a) (blake2_key_block a kk k) s;
(Seq.equal) { Lib.LoopCombinators.eq_repeati0 nb (blake2_update1 a 0 (blake2_key_block a kk k)) s }
blake2_update_blocks a 0 (blake2_key_block a kk k) s;
(Seq.equal) { Seq.append_empty_r (blake2_key_block a kk k) }
blake2_update_blocks a 0 (blake2_key_block a kk k `Seq.append` Seq.empty) s;
(Seq.equal) { Seq.lemma_empty d }
blake2_update_blocks a 0 (blake2_key_block a kk k `Seq.append` d) s;
(Seq.equal) { }
blake2_update' a kk k d s;
};
()
else
let (nb,rem) = split a (length (blake2_key_block a kk k `Seq.append` d)) in
calc (Seq.equal) {
blake2_update a kk k d s;
(Seq.equal) {}
blake2_update_blocks a (size_block a) d (blake2_update_key a kk k ll s);
(Seq.equal) {}
blake2_update_blocks a (size_block a) d (blake2_update_block a false (size_block a) (blake2_key_block a kk k) s);
(Seq.equal) { lemma_unfold_update_blocks a (blake2_key_block a kk k) d s }
blake2_update_blocks a 0 (blake2_key_block a kk k `Seq.append` d) s;
} | {
"file_name": "specs/lemmas/Spec.Blake2.Alternative.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 5,
"end_line": 136,
"start_col": 0,
"start_line": 100
} | module Spec.Blake2.Alternative
open Spec.Blake2
open FStar.Mul
open Lib.IntTypes
open Lib.RawIntTypes
open Lib.Sequence
open Lib.ByteSequence
open Lib.LoopCombinators
module Lems = Lib.Sequence.Lemmas
module UpdateMulti = Lib.UpdateMulti
#set-options "--fuel 0 --ifuel 0 --z3rlimit 50"
val lemma_shift_update_last:
a:alg
-> rem: nat
-> b:block_s a
-> d:bytes{length d + (size_block a) <= max_limb a /\ rem <= length d /\ rem <= size_block a}
-> s:state a ->
Lemma (
blake2_update_last a 0 rem (b `Seq.append` d) s ==
blake2_update_last a (size_block a) rem d s
)
let lemma_shift_update_last a rem b d s =
let m = b `Seq.append` d in
assert (Seq.slice m (length m - rem) (length m) `Seq.equal` Seq.slice d (length d - rem) (length d));
assert (get_last_padded_block a (b `Seq.append` d) rem == get_last_padded_block a d rem)
val lemma_update1_shift:
a:alg
-> b:block_s a
-> d:bytes{length d + (size_block a) <= max_limb a}
-> i:nat{i < length d / size_block a /\ (size_block a) + length d <= max_limb a}
-> s:state a ->
Lemma (
blake2_update1 a 0 (b `Seq.append` d) (i + 1) s == blake2_update1 a (size_block a) d i s
)
let lemma_update1_shift a b d i s =
assert (get_blocki a (b `Seq.append` d) (i + 1) `Seq.equal` get_blocki a d i)
val repeati_update1:
a:alg
-> b:block_s a
-> d:bytes{length d + (size_block a) <= max_limb a}
-> nb:nat{nb > 0 /\ nb <= length (b `Seq.append` d) / size_block a}
-> s:state a ->
Lemma (
repeati nb (blake2_update1 a 0 (b `Seq.append` d)) s ==
repeati (nb - 1) (blake2_update1 a (size_block a) d) (blake2_update_block a false (size_block a) b s)
)
#push-options "--z3rlimit 100"
let repeati_update1 a b d nb s =
let f = blake2_update1 a 0 (b `Seq.append` d) in
let f' = blake2_update1 a (size_block a) d in
let s' = blake2_update_block a false (size_block a) b s in
Classical.forall_intro_2 (lemma_update1_shift a b d);
assert (forall i s. f (i + 1) s == f' i s);
Lems.repeati_right_shift (nb - 1) f' f s;
assert (get_blocki a (b `Seq.append` d) 0 `Seq.equal` b);
assert (s' == f 0 s)
#pop-options
val lemma_unfold_update_blocks:
a:alg
-> b:block_s a
-> d:bytes{length d > 0 /\ length d + (size_block a) <= max_limb a}
-> s:state a ->
Lemma (
blake2_update_blocks a 0 (b `Seq.append` d) s ==
blake2_update_blocks a (size_block a) d (blake2_update_block a false (size_block a) b s)
)
val split_one_more_block:
a:alg
-> b:block_s a
-> d:bytes{length d > 0} ->
Lemma (
let nb, rem = split a (length (b `Seq.append` d)) in
let nb', rem' = split a (length d) in
nb == nb' + 1 /\ rem == rem')
let split_one_more_block a b d = ()
let lemma_unfold_update_blocks a b d s =
let nb, rem = split a (length (b `Seq.append` d)) in
let nb', rem' = split a (length d) in
// The condition `length d > 0` is needed for this assertion, otherwise,
// we would have rem = size_block a and rem' = 0
split_one_more_block a b d;
repeati_update1 a b d nb s;
let s_int = repeati nb (blake2_update1 a 0 (b `Seq.append` d)) s in
lemma_shift_update_last a rem b d s_int | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.RawIntTypes.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": true,
"source_file": "Spec.Blake2.Alternative.fst"
} | [
{
"abbrev": true,
"full_module": "Lib.UpdateMulti",
"short_module": "UpdateMulti"
},
{
"abbrev": true,
"full_module": "Lib.Sequence.Lemmas",
"short_module": "Lems"
},
{
"abbrev": false,
"full_module": "Lib.LoopCombinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.RawIntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.ByteSequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Spec.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 1000,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Spec.Blake2.Definitions.alg ->
kk: Lib.IntTypes.size_nat{kk <= Spec.Blake2.Definitions.max_key a} ->
k: Lib.ByteSequence.lbytes kk ->
d:
Lib.ByteSequence.bytes
{ (match kk = 0 with
| true -> Lib.Sequence.length d <= Spec.Blake2.Definitions.max_limb a
| _ ->
Lib.Sequence.length d + Spec.Blake2.Definitions.size_block a <=
Spec.Blake2.Definitions.max_limb a)
<:
Type0 } ->
s: Spec.Blake2.Definitions.state a
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.equal (Spec.Blake2.blake2_update a kk k d s)
(Spec.Blake2.Alternative.blake2_update' a kk k d s)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Spec.Blake2.Definitions.alg",
"Lib.IntTypes.size_nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_key",
"Lib.ByteSequence.lbytes",
"Lib.ByteSequence.bytes",
"Prims.op_Equality",
"Prims.int",
"Lib.Sequence.length",
"Lib.IntTypes.uint_t",
"Lib.IntTypes.U8",
"Lib.IntTypes.SEC",
"Spec.Blake2.Definitions.max_limb",
"Prims.bool",
"Prims.op_Addition",
"Spec.Blake2.Definitions.size_block",
"Spec.Blake2.Definitions.state",
"Prims.unit",
"Prims._assert",
"FStar.Seq.Base.equal",
"Lib.IntTypes.int_t",
"FStar.Seq.Base.append",
"FStar.Seq.Base.empty",
"Prims.nat",
"FStar.Calc.calc_finish",
"FStar.Seq.Base.seq",
"Spec.Blake2.Definitions.row",
"Spec.Blake2.blake2_update",
"Spec.Blake2.Alternative.blake2_update'",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"Spec.Blake2.blake2_update_blocks",
"Lib.IntTypes.uint8",
"Spec.Blake2.blake2_key_block",
"Spec.Blake2.blake2_update_block",
"Spec.Blake2.blake2_update_key",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"Lib.LoopCombinators.eq_repeati0",
"Spec.Blake2.blake2_update1",
"FStar.Seq.Base.append_empty_r",
"FStar.Seq.Base.lemma_empty",
"FStar.Pervasives.Native.tuple2",
"Prims.eq2",
"Prims.op_Multiply",
"Spec.Blake2.split",
"Spec.Blake2.Alternative.lemma_unfold_update_blocks",
"Lib.Sequence.seq",
"Prims.op_GreaterThan"
] | [] | false | false | true | false | false | let lemma_spec_equivalence_update a kk k d s =
| let ll = length d in
let key_block:bytes = if kk > 0 then blake2_key_block a kk k else Seq.empty in
if kk = 0
then
(assert (key_block `Seq.equal` Seq.empty);
assert ((key_block `Seq.append` d) `Seq.equal` d);
())
else
if ll = 0
then
let nb, rem = split a (length (blake2_key_block a kk k)) in
calc (Seq.equal) {
blake2_update a kk k d s;
(Seq.equal) { () }
blake2_update_key a kk k ll s;
(Seq.equal) { () }
blake2_update_block a true (size_block a) (blake2_key_block a kk k) s;
(Seq.equal) { Lib.LoopCombinators.eq_repeati0 nb
(blake2_update1 a 0 (blake2_key_block a kk k))
s }
blake2_update_blocks a 0 (blake2_key_block a kk k) s;
(Seq.equal) { Seq.append_empty_r (blake2_key_block a kk k) }
blake2_update_blocks a 0 ((blake2_key_block a kk k) `Seq.append` Seq.empty) s;
(Seq.equal) { Seq.lemma_empty d }
blake2_update_blocks a 0 ((blake2_key_block a kk k) `Seq.append` d) s;
(Seq.equal) { () }
blake2_update' a kk k d s;
};
()
else
let nb, rem = split a (length ((blake2_key_block a kk k) `Seq.append` d)) in
calc (Seq.equal) {
blake2_update a kk k d s;
(Seq.equal) { () }
blake2_update_blocks a (size_block a) d (blake2_update_key a kk k ll s);
(Seq.equal) { () }
blake2_update_blocks a
(size_block a)
d
(blake2_update_block a false (size_block a) (blake2_key_block a kk k) s);
(Seq.equal) { lemma_unfold_update_blocks a (blake2_key_block a kk k) d s }
blake2_update_blocks a 0 ((blake2_key_block a kk k) `Seq.append` d) s;
} | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_contains_fact | val insert_contains_fact : Prims.logical | let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 225,
"start_col": 0,
"start_line": 223
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.insert"
] | [] | false | false | false | true | true | let insert_contains_fact =
| forall (a: eqtype) (s: set a) (x: a) (y: a). {:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s) | false |
|
FStar.OrdSet.fsti | FStar.OrdSet.inv | val inv (#a: _) (c: condition a) : (z: condition a {forall x. c x = not (z x)}) | val inv (#a: _) (c: condition a) : (z: condition a {forall x. c x = not (z x)}) | let inv #a (c: condition a) : (z:condition a{forall x. c x = not (z x)}) = fun x -> not (c x) | {
"file_name": "ulib/experimental/FStar.OrdSet.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 93,
"end_line": 205,
"start_col": 0,
"start_line": 205
} | (*
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 FStar.OrdSet
type total_order (a:eqtype) (f: (a -> a -> Tot bool)) =
(forall a1 a2. (f a1 a2 /\ f a2 a1) ==> a1 = a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
type cmp (a:eqtype) = f:(a -> a -> Tot bool){total_order a f}
let rec sorted (#a:eqtype) (f:cmp a) (l:list a) : Tot bool =
match l with
| [] -> true
| x::[] -> true
| x::y::tl -> f x y && x <> y && sorted f (y::tl)
val ordset (a:eqtype) (f:cmp a) : Type0
val hasEq_ordset: a:eqtype -> f:cmp a
-> Lemma (requires (True)) (ensures (hasEq (ordset a f)))
[SMTPat (hasEq (ordset a f))]
val empty : #a:eqtype -> #f:cmp a -> Tot (ordset a f)
val tail (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : ordset a f
val head (#a:eqtype) (#f:cmp a) (s:ordset a f{s<>empty}) : a
val mem : #a:eqtype -> #f:cmp a -> a -> s:ordset a f -> Tot bool
(* currying-friendly version of mem, ready to be used as a lambda *)
unfold let mem_of #a #f (s:ordset a f) x = mem x s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty})
: Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
(*
liat is the reverse of tail, i.e. a list of all but the last element.
A shortcut to (fst (unsnoc s)), which as a word is composed
in a remarkably similar fashion.
*)
val liat (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (l:ordset a f{
(forall x. mem x l = (mem x s && (x <> last s))) /\
(if tail s <> empty then (l <> empty) && (head s = head l) else true)
})
val unsnoc (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) : Tot (p:(ordset a f * a){
p = (liat s, last s)
})
val as_list (#a:eqtype) (#f:cmp a) (s:ordset a f) : Tot (l:list a{
sorted f l /\
(forall x. (List.Tot.mem x l = mem x s))
})
val distinct (#a:eqtype) (f:cmp a) (l: list a) : Pure (ordset a f)
(requires True) (ensures fun z -> forall x. (mem x z = List.Tot.Base.mem x l))
val union : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val intersect : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val choose : #a:eqtype -> #f:cmp a -> s:ordset a f -> Tot (option a)
val remove : #a:eqtype -> #f:cmp a -> a -> ordset a f -> Tot (ordset a f)
val size : #a:eqtype -> #f:cmp a -> ordset a f -> Tot nat
val subset : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let superset #a #f (s1 s2: ordset a f) : Tot bool = subset s2 s1
val singleton : #a:eqtype -> #f:cmp a -> a -> Tot (ordset a f)
val minus : #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot (ordset a f)
val strict_subset: #a:eqtype -> #f:cmp a -> ordset a f -> ordset a f -> Tot bool
let strict_superset #a #f (s1 s2: ordset a f) : Tot bool = strict_subset s2 s1
let disjoint #a #f (s1 s2 : ordset a f) : Tot bool = intersect s1 s2 = empty
let equal (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) : Tot prop =
forall x. mem #_ #f x s1 = mem #_ #f x s2
val eq_lemma: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires (equal s1 s2))
(ensures (s1 = s2))
[SMTPat (equal s1 s2)]
val mem_empty: #a:eqtype -> #f:cmp a -> x:a
-> Lemma (requires True) (ensures (not (mem #a #f x (empty #a #f))))
[SMTPat (mem #a #f x (empty #a #f))]
val mem_singleton: #a:eqtype -> #f:cmp a -> x:a -> y:a
-> Lemma (requires True)
(ensures (mem #a #f y (singleton #a #f x)) = (x = y))
[SMTPat (mem #a #f y (singleton #a #f x))]
val mem_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f -> x:a
-> Lemma (requires True)
(ensures (mem #a #f x (union #a #f s1 s2) =
(mem #a #f x s1 || mem #a #f x s2)))
[SMTPat (mem #a #f x (union #a #f s1 s2))]
val mem_intersect: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f -> x:a
-> Lemma (requires True)
(ensures (mem #a #f x (intersect s1 s2) =
(mem #a #f x s1 && mem #a #f x s2)))
[SMTPat (mem #a #f x (intersect #a #f s1 s2))]
val mem_subset: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures (subset #a #f s1 s2 <==>
(forall x. mem #a #f x s1 ==> mem #a #f x s2)))
[SMTPat (subset #a #f s1 s2)]
val choose_empty: #a:eqtype -> #f:cmp a
-> Lemma (requires True) (ensures (None? (choose #a #f (empty #a #f))))
[SMTPat (choose #a #f (empty #a #f))]
(* TODO: FIXME: Pattern does not contain all quantified vars *)
val choose_s: #a:eqtype -> #f:cmp a -> s:ordset a f
-> Lemma (requires (not (s = (empty #a #f))))
(ensures (Some? (choose #a #f s) /\
s = union #a #f (singleton #a #f (Some?.v (choose #a #f s)))
(remove #a #f (Some?.v (choose #a #f s)) s)))
[SMTPat (choose #a #f s)]
val mem_remove: #a:eqtype -> #f:cmp a -> x:a -> y:a -> s:ordset a f
-> Lemma (requires True)
(ensures (mem #a #f x (remove #a #f y s) =
(mem #a #f x s && not (x = y))))
[SMTPat (mem #a #f x (remove #a #f y s))]
val eq_remove: #a:eqtype -> #f:cmp a -> x:a -> s:ordset a f
-> Lemma (requires (not (mem #a #f x s)))
(ensures (s = remove #a #f x s))
[SMTPat (remove #a #f x s)]
val size_empty: #a:eqtype -> #f:cmp a -> s:ordset a f
-> Lemma (requires True) (ensures ((size #a #f s = 0) = (s = empty #a #f)))
[SMTPat (size #a #f s)]
val size_remove: #a:eqtype -> #f:cmp a -> y:a -> s:ordset a f
-> Lemma (requires (mem #a #f y s))
(ensures (size #a #f s = size #a #f (remove #a #f y s) + 1))
[SMTPat (size #a #f (remove #a #f y s))]
val size_singleton: #a:eqtype -> #f:cmp a -> x:a
-> Lemma (requires True) (ensures (size #a #f (singleton #a #f x) = 1))
[SMTPat (size #a #f (singleton #a #f x))]
val subset_size: #a:eqtype -> #f:cmp a -> x:ordset a f -> y:ordset a f
-> Lemma (requires (subset #a #f x y))
(ensures (size #a #f x <= size #a #f y))
[SMTPat (subset #a #f x y)]
(**********)
val size_union: #a:eqtype -> #f:cmp a -> s1:ordset a f -> s2:ordset a f
-> Lemma (requires True)
(ensures ((size #a #f (union #a #f s1 s2) >= size #a #f s1) &&
(size #a #f (union #a #f s1 s2) >= size #a #f s2)))
[SMTPat (size #a #f (union #a #f s1 s2))]
(**********)
val fold (#a:eqtype) (#acc:Type) (#f:cmp a) (g:acc -> a -> acc) (init:acc) (s:ordset a f)
: Tot acc
val map (#a #b:eqtype) (#fa:cmp a) (#fb:cmp b) (g:a -> b) (sa:ordset a fa)
: Pure (ordset b fb)
(requires (forall x y. (x `fa` y ==> g x `fb` g y) /\ (x = y <==> g x = g y)))
(ensures (fun sb -> (size sb <= size sa) /\
(as_list sb == FStar.List.Tot.map g (as_list sa)) /\
(let sa = as_list sa in
let sb = as_list sb in
Cons? sb ==> Cons? sa /\ Cons?.hd sb == g (Cons?.hd sa))))
val lemma_strict_subset_size (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires (strict_subset s1 s2))
(ensures (subset s1 s2 /\ size s1 < size s2))
[SMTPat (strict_subset s1 s2)]
val lemma_minus_mem (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f) (x:a)
: Lemma (requires True) (ensures (mem x (minus s1 s2) = (mem x s1 && not (mem x s2))))
[SMTPat (mem x (minus s1 s2))]
val lemma_strict_subset_exists_diff (#a:eqtype) (#f:cmp a) (s1:ordset a f) (s2:ordset a f)
: Lemma (requires subset s1 s2)
(ensures (strict_subset s1 s2) <==> (exists x. (mem x s2 /\ not (mem x s1))))
type condition a = a -> bool | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Set.fsti.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "FStar.OrdSet.fsti"
} | [
{
"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
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | c: FStar.OrdSet.condition a
-> z: FStar.OrdSet.condition a {forall (x: a). c x = Prims.op_Negation (z x)} | Prims.Tot | [
"total"
] | [] | [
"FStar.OrdSet.condition",
"Prims.op_Negation",
"Prims.bool",
"Prims.l_Forall",
"Prims.b2t",
"Prims.op_Equality"
] | [] | false | false | false | false | false | let inv #a (c: condition a) : (z: condition a {forall x. c x = not (z x)}) =
| fun x -> not (c x) | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_contains_element_from_second_argument_fact | val union_contains_element_from_second_argument_fact : Prims.logical | let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 270,
"start_col": 0,
"start_line": 268
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.union"
] | [] | false | false | false | true | true | let union_contains_element_from_second_argument_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (y: a). {:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_contains_fact | val union_contains_fact : Prims.logical | let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 252,
"start_col": 0,
"start_line": 250
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.union",
"Prims.l_or"
] | [] | false | false | false | true | true | let union_contains_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (o: a). {:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_nonmember_cardinality_fact | val insert_nonmember_cardinality_fact : Prims.logical | let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 66,
"end_line": 243,
"start_col": 0,
"start_line": 241
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Negation",
"FStar.FiniteSet.Base.mem",
"Prims.op_Equality",
"Prims.int",
"FStar.FiniteSet.Base.cardinality",
"FStar.FiniteSet.Base.insert",
"Prims.op_Addition"
] | [] | false | false | false | true | true | let insert_nonmember_cardinality_fact =
| forall (a: eqtype) (s: set a) (x: a). {:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_of_disjoint_fact | val union_of_disjoint_fact : Prims.logical | let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 93,
"end_line": 281,
"start_col": 0,
"start_line": 279
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"FStar.FiniteSet.Base.disjoint",
"Prims.l_and",
"Prims.eq2",
"FStar.FiniteSet.Base.difference",
"FStar.FiniteSet.Base.union"
] | [] | false | false | false | true | true | let union_of_disjoint_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_contains_element_from_first_argument_fact | val union_contains_element_from_first_argument_fact : Prims.logical | let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 261,
"start_col": 0,
"start_line": 259
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.union"
] | [] | false | false | false | true | true | let union_contains_element_from_first_argument_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (y: a). {:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.intersection_contains_fact | val intersection_contains_fact : Prims.logical | let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 56,
"end_line": 290,
"start_col": 0,
"start_line": 288
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.intersection",
"Prims.l_and"
] | [] | false | false | false | true | true | let intersection_contains_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (o: a). {:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_idempotent_right_fact | val union_idempotent_right_fact : Prims.logical | let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 299,
"start_col": 0,
"start_line": 297
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.eq2",
"FStar.FiniteSet.Base.union"
] | [] | false | false | false | true | true | let union_idempotent_right_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.intersection_idempotent_left_fact | val intersection_idempotent_left_fact : Prims.logical | let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 62,
"end_line": 326,
"start_col": 0,
"start_line": 324
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.eq2",
"FStar.FiniteSet.Base.intersection"
] | [] | false | false | false | true | true | let intersection_idempotent_left_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.intersection_idempotent_right_fact | val intersection_idempotent_right_fact : Prims.logical | let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 62,
"end_line": 317,
"start_col": 0,
"start_line": 315
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.eq2",
"FStar.FiniteSet.Base.intersection"
] | [] | false | false | false | true | true | let intersection_idempotent_right_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.difference_contains_fact | val difference_contains_fact : Prims.logical | let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 60,
"end_line": 344,
"start_col": 0,
"start_line": 342
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"FStar.FiniteSet.Base.difference",
"Prims.l_and",
"Prims.op_Negation"
] | [] | false | false | false | true | true | let difference_contains_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (o: a). {:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.union_idempotent_left_fact | val union_idempotent_left_fact : Prims.logical | let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 308,
"start_col": 0,
"start_line": 306
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.eq2",
"FStar.FiniteSet.Base.union"
] | [] | false | false | false | true | true | let union_idempotent_left_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.difference_doesnt_include_fact | val difference_doesnt_include_fact : Prims.logical | let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2)) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 47,
"end_line": 353,
"start_col": 0,
"start_line": 351
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] ); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"Prims.op_Negation",
"FStar.FiniteSet.Base.difference"
] | [] | false | false | false | true | true | let difference_doesnt_include_fact =
| forall (a: eqtype) (s1: set a) (s2: set a) (y: a). {:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2)) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.intersection_cardinality_fact | val intersection_cardinality_fact : Prims.logical | let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 98,
"end_line": 335,
"start_col": 0,
"start_line": 333
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Addition",
"FStar.FiniteSet.Base.cardinality",
"FStar.FiniteSet.Base.union",
"FStar.FiniteSet.Base.intersection"
] | [] | false | false | false | true | true | let intersection_cardinality_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.difference_cardinality_fact | val difference_cardinality_fact : Prims.logical | let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 89,
"end_line": 367,
"start_col": 0,
"start_line": 364
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b))); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_and",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Addition",
"FStar.FiniteSet.Base.cardinality",
"FStar.FiniteSet.Base.difference",
"FStar.FiniteSet.Base.intersection",
"FStar.FiniteSet.Base.union",
"Prims.op_Subtraction"
] | [] | false | false | false | true | true | let difference_cardinality_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) =
cardinality (union s1 s2) /\
cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.equal_extensionality_fact | val equal_extensionality_fact : Prims.logical | let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2 | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 394,
"start_col": 0,
"start_line": 392
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"FStar.FiniteSet.Base.equal",
"Prims.eq2"
] | [] | false | false | false | true | true | let equal_extensionality_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern equal s1 s2} equal s1 s2 ==> s1 == s2 | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_remove_fact | val insert_remove_fact : Prims.logical | let insert_remove_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern insert x (remove x s)}
mem x s = true ==> insert x (remove x s) == s | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 409,
"start_col": 0,
"start_line": 407
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b);
let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T :: { Set#Disjoint(a,b) }
/// Set#Disjoint(a,b) <==> (forall o: T :: {a[o]} {b[o]} !a[o] || !b[o]));
let disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} not (mem o s1) \/ not (mem o s2))
/// We add a few more facts for the utility function `remove` and for `set_as_list`: | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.FiniteSet.Base.mem",
"Prims.eq2",
"FStar.FiniteSet.Base.insert",
"FStar.FiniteSet.Base.remove"
] | [] | false | false | false | true | true | let insert_remove_fact =
| forall (a: eqtype) (x: a) (s: set a). {:pattern insert x (remove x s)}
mem x s = true ==> insert x (remove x s) == s | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.equal_fact | val equal_fact : Prims.logical | let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 86,
"end_line": 385,
"start_col": 0,
"start_line": 383
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o])); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"FStar.FiniteSet.Base.equal",
"Prims.b2t",
"FStar.FiniteSet.Base.mem"
] | [] | false | false | false | true | true | let equal_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern equal s1 s2}
equal s1 s2 <==> (forall o. {:pattern mem o s1\/mem o s2} mem o s1 <==> mem o s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.subset_fact | val subset_fact : Prims.logical | let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 86,
"end_line": 376,
"start_col": 0,
"start_line": 374
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o])); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"FStar.FiniteSet.Base.subset",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem"
] | [] | false | false | false | true | true | let subset_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern subset s1 s2}
subset s1 s2 <==> (forall o. {:pattern mem o s1\/mem o s2} mem o s1 ==> mem o s2) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.insert_member_cardinality_fact | val insert_member_cardinality_fact : Prims.logical | let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 56,
"end_line": 234,
"start_col": 0,
"start_line": 232
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a)); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"FStar.FiniteSet.Base.mem",
"Prims.op_Equality",
"Prims.nat",
"FStar.FiniteSet.Base.cardinality",
"FStar.FiniteSet.Base.insert"
] | [] | false | false | false | true | true | let insert_member_cardinality_fact =
| forall (a: eqtype) (s: set a) (x: a). {:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.disjoint_fact | val disjoint_fact : Prims.logical | let disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} not (mem o s1) \/ not (mem o s2)) | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 99,
"end_line": 403,
"start_col": 0,
"start_line": 401
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b);
let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T :: { Set#Disjoint(a,b) }
/// Set#Disjoint(a,b) <==> (forall o: T :: {a[o]} {b[o]} !a[o] || !b[o])); | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_iff",
"FStar.FiniteSet.Base.disjoint",
"Prims.l_or",
"Prims.b2t",
"Prims.op_Negation",
"FStar.FiniteSet.Base.mem"
] | [] | false | false | false | true | true | let disjoint_fact =
| forall (a: eqtype) (s1: set a) (s2: set a). {:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o. {:pattern mem o s1\/mem o s2} not (mem o s1) \/ not (mem o s2)) | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.remove_insert_fact | val remove_insert_fact : Prims.logical | let remove_insert_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern remove x (insert x s)}
mem x s = false ==> remove x (insert x s) == s | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 413,
"start_col": 0,
"start_line": 411
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b);
let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T :: { Set#Disjoint(a,b) }
/// Set#Disjoint(a,b) <==> (forall o: T :: {a[o]} {b[o]} !a[o] || !b[o]));
let disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} not (mem o s1) \/ not (mem o s2))
/// We add a few more facts for the utility function `remove` and for `set_as_list`:
let insert_remove_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern insert x (remove x s)}
mem x s = true ==> insert x (remove x s) == s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Equality",
"Prims.bool",
"FStar.FiniteSet.Base.mem",
"Prims.eq2",
"FStar.FiniteSet.Base.remove",
"FStar.FiniteSet.Base.insert"
] | [] | false | false | false | true | true | let remove_insert_fact =
| forall (a: eqtype) (x: a) (s: set a). {:pattern remove x (insert x s)}
mem x s = false ==> remove x (insert x s) == s | false |
|
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.all_finite_set_facts | val all_finite_set_facts : Prims.logical | let all_finite_set_facts =
empty_set_contains_no_elements_fact
/\ length_zero_fact
/\ singleton_contains_argument_fact
/\ singleton_contains_fact
/\ singleton_cardinality_fact
/\ insert_fact
/\ insert_contains_argument_fact
/\ insert_contains_fact
/\ insert_member_cardinality_fact
/\ insert_nonmember_cardinality_fact
/\ union_contains_fact
/\ union_contains_element_from_first_argument_fact
/\ union_contains_element_from_second_argument_fact
/\ union_of_disjoint_fact
/\ intersection_contains_fact
/\ union_idempotent_right_fact
/\ union_idempotent_left_fact
/\ intersection_idempotent_right_fact
/\ intersection_idempotent_left_fact
/\ intersection_cardinality_fact
/\ difference_contains_fact
/\ difference_doesnt_include_fact
/\ difference_cardinality_fact
/\ subset_fact
/\ equal_fact
/\ equal_extensionality_fact
/\ disjoint_fact
/\ insert_remove_fact
/\ remove_insert_fact
/\ set_as_list_cardinality_fact | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 454,
"start_col": 0,
"start_line": 424
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b);
let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T :: { Set#Disjoint(a,b) }
/// Set#Disjoint(a,b) <==> (forall o: T :: {a[o]} {b[o]} !a[o] || !b[o]));
let disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} not (mem o s1) \/ not (mem o s2))
/// We add a few more facts for the utility function `remove` and for `set_as_list`:
let insert_remove_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern insert x (remove x s)}
mem x s = true ==> insert x (remove x s) == s
let remove_insert_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern remove x (insert x s)}
mem x s = false ==> remove x (insert x s) == s
let set_as_list_cardinality_fact =
forall (a: eqtype) (s: set a).{:pattern FLT.length (set_as_list s)}
FLT.length (set_as_list s) = cardinality s
(**
The predicate `all_finite_set_facts` collects all the Dafny finite-set axioms.
One can bring all these facts into scope with `all_finite_set_facts_lemma ()`.
**) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_and",
"FStar.FiniteSet.Base.empty_set_contains_no_elements_fact",
"FStar.FiniteSet.Base.length_zero_fact",
"FStar.FiniteSet.Base.singleton_contains_argument_fact",
"FStar.FiniteSet.Base.singleton_contains_fact",
"FStar.FiniteSet.Base.singleton_cardinality_fact",
"FStar.FiniteSet.Base.insert_fact",
"FStar.FiniteSet.Base.insert_contains_argument_fact",
"FStar.FiniteSet.Base.insert_contains_fact",
"FStar.FiniteSet.Base.insert_member_cardinality_fact",
"FStar.FiniteSet.Base.insert_nonmember_cardinality_fact",
"FStar.FiniteSet.Base.union_contains_fact",
"FStar.FiniteSet.Base.union_contains_element_from_first_argument_fact",
"FStar.FiniteSet.Base.union_contains_element_from_second_argument_fact",
"FStar.FiniteSet.Base.union_of_disjoint_fact",
"FStar.FiniteSet.Base.intersection_contains_fact",
"FStar.FiniteSet.Base.union_idempotent_right_fact",
"FStar.FiniteSet.Base.union_idempotent_left_fact",
"FStar.FiniteSet.Base.intersection_idempotent_right_fact",
"FStar.FiniteSet.Base.intersection_idempotent_left_fact",
"FStar.FiniteSet.Base.intersection_cardinality_fact",
"FStar.FiniteSet.Base.difference_contains_fact",
"FStar.FiniteSet.Base.difference_doesnt_include_fact",
"FStar.FiniteSet.Base.difference_cardinality_fact",
"FStar.FiniteSet.Base.subset_fact",
"FStar.FiniteSet.Base.equal_fact",
"FStar.FiniteSet.Base.equal_extensionality_fact",
"FStar.FiniteSet.Base.disjoint_fact",
"FStar.FiniteSet.Base.insert_remove_fact",
"FStar.FiniteSet.Base.remove_insert_fact",
"FStar.FiniteSet.Base.set_as_list_cardinality_fact"
] | [] | false | false | false | true | true | let all_finite_set_facts =
| empty_set_contains_no_elements_fact /\ length_zero_fact /\ singleton_contains_argument_fact /\
singleton_contains_fact /\ singleton_cardinality_fact /\ insert_fact /\
insert_contains_argument_fact /\ insert_contains_fact /\ insert_member_cardinality_fact /\
insert_nonmember_cardinality_fact /\ union_contains_fact /\
union_contains_element_from_first_argument_fact /\ union_contains_element_from_second_argument_fact /\
union_of_disjoint_fact /\ intersection_contains_fact /\ union_idempotent_right_fact /\
union_idempotent_left_fact /\ intersection_idempotent_right_fact /\
intersection_idempotent_left_fact /\ intersection_cardinality_fact /\ difference_contains_fact /\
difference_doesnt_include_fact /\ difference_cardinality_fact /\ subset_fact /\ equal_fact /\
equal_extensionality_fact /\ disjoint_fact /\ insert_remove_fact /\ remove_insert_fact /\
set_as_list_cardinality_fact | false |
|
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.proj_point_to_list | val proj_point_to_list (p: S.proj_point) : point_list | val proj_point_to_list (p: S.proj_point) : point_list | let proj_point_to_list (p:S.proj_point) : point_list =
[@inline_let] let (px, py, pz) = p in
[@inline_let] let pxM = SM.to_mont px in
[@inline_let] let pyM = SM.to_mont py in
[@inline_let] let pzM = SM.to_mont pz in
FL.(felem_to_list pxM @ felem_to_list pyM @ felem_to_list pzM) | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 64,
"end_line": 39,
"start_col": 0,
"start_line": 34
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3]
inline_for_extraction noextract
let felem_list = x:list uint64{FL.length x == 4}
inline_for_extraction noextract
let point_list = x:list uint64{FL.length x == 12}
inline_for_extraction noextract
let felem_to_list (x:S.felem) : felem_list =
[@inline_let] let x0 = x % pow2 64 in
[@inline_let] let x1 = x / pow2 64 % pow2 64 in
[@inline_let] let x2 = x / pow2 128 % pow2 64 in
[@inline_let] let x3 = x / pow2 192 % pow2 64 in
[@inline_let] let r = create4 (u64 x0) (u64 x1) (u64 x2) (u64 x3) in
assert_norm (FL.length r = 4);
r | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Spec.P256.PointOps.proj_point -> Hacl.Spec.P256.PrecompTable.point_list | Prims.Tot | [
"total"
] | [] | [
"Spec.P256.PointOps.proj_point",
"Prims.nat",
"FStar.List.Tot.Base.op_At",
"Lib.IntTypes.uint64",
"Hacl.Spec.P256.PrecompTable.felem_to_list",
"Spec.P256.PointOps.felem",
"Hacl.Spec.P256.Montgomery.to_mont",
"Hacl.Spec.P256.PrecompTable.point_list"
] | [] | false | false | false | true | false | let proj_point_to_list (p: S.proj_point) : point_list =
| [@@ inline_let ]let px, py, pz = p in
[@@ inline_let ]let pxM = SM.to_mont px in
[@@ inline_let ]let pyM = SM.to_mont py in
[@@ inline_let ]let pzM = SM.to_mont pz in
let open FL in felem_to_list pxM @ felem_to_list pyM @ felem_to_list pzM | false |
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.point_inv_list | val point_inv_list : p: Hacl.Spec.P256.PrecompTable.point_list -> Prims.logical | let point_inv_list (p:point_list) =
let x = Seq.seq_of_list p <: lseq uint64 12 in
point_inv_seq x | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 17,
"end_line": 44,
"start_col": 0,
"start_line": 42
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3]
inline_for_extraction noextract
let felem_list = x:list uint64{FL.length x == 4}
inline_for_extraction noextract
let point_list = x:list uint64{FL.length x == 12}
inline_for_extraction noextract
let felem_to_list (x:S.felem) : felem_list =
[@inline_let] let x0 = x % pow2 64 in
[@inline_let] let x1 = x / pow2 64 % pow2 64 in
[@inline_let] let x2 = x / pow2 128 % pow2 64 in
[@inline_let] let x3 = x / pow2 192 % pow2 64 in
[@inline_let] let r = create4 (u64 x0) (u64 x1) (u64 x2) (u64 x3) in
assert_norm (FL.length r = 4);
r
inline_for_extraction noextract
let proj_point_to_list (p:S.proj_point) : point_list =
[@inline_let] let (px, py, pz) = p in
[@inline_let] let pxM = SM.to_mont px in
[@inline_let] let pyM = SM.to_mont py in
[@inline_let] let pzM = SM.to_mont pz in
FL.(felem_to_list pxM @ felem_to_list pyM @ felem_to_list pzM) | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Spec.P256.PrecompTable.point_list -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Hacl.Spec.P256.PrecompTable.point_list",
"Hacl.Impl.P256.Point.point_inv_seq",
"Lib.Sequence.lseq",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.Seq.Base.seq_of_list",
"Lib.IntTypes.uint64",
"Prims.logical"
] | [] | false | false | false | true | true | let point_inv_list (p: point_list) =
| let x = Seq.seq_of_list p <: lseq uint64 12 in
point_inv_seq x | false |
|
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.felem_to_list | val felem_to_list (x: S.felem) : felem_list | val felem_to_list (x: S.felem) : felem_list | let felem_to_list (x:S.felem) : felem_list =
[@inline_let] let x0 = x % pow2 64 in
[@inline_let] let x1 = x / pow2 64 % pow2 64 in
[@inline_let] let x2 = x / pow2 128 % pow2 64 in
[@inline_let] let x3 = x / pow2 192 % pow2 64 in
[@inline_let] let r = create4 (u64 x0) (u64 x1) (u64 x2) (u64 x3) in
assert_norm (FL.length r = 4);
r | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 31,
"start_col": 0,
"start_line": 24
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3]
inline_for_extraction noextract
let felem_list = x:list uint64{FL.length x == 4}
inline_for_extraction noextract
let point_list = x:list uint64{FL.length x == 12} | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Spec.P256.PointOps.felem -> Hacl.Spec.P256.PrecompTable.felem_list | Prims.Tot | [
"total"
] | [] | [
"Spec.P256.PointOps.felem",
"Prims.unit",
"FStar.Pervasives.assert_norm",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.List.Tot.Base.length",
"Lib.IntTypes.uint64",
"Prims.list",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"Hacl.Spec.P256.PrecompTable.create4",
"Lib.IntTypes.u64",
"Prims.op_Modulus",
"Prims.op_Division",
"Prims.pow2",
"Hacl.Spec.P256.PrecompTable.felem_list"
] | [] | false | false | false | true | false | let felem_to_list (x: S.felem) : felem_list =
| [@@ inline_let ]let x0 = x % pow2 64 in
[@@ inline_let ]let x1 = x / pow2 64 % pow2 64 in
[@@ inline_let ]let x2 = x / pow2 128 % pow2 64 in
[@@ inline_let ]let x3 = x / pow2 192 % pow2 64 in
[@@ inline_let ]let r = create4 (u64 x0) (u64 x1) (u64 x2) (u64 x3) in
assert_norm (FL.length r = 4);
r | false |
FStar.FiniteSet.Base.fsti | FStar.FiniteSet.Base.set_as_list_cardinality_fact | val set_as_list_cardinality_fact : Prims.logical | let set_as_list_cardinality_fact =
forall (a: eqtype) (s: set a).{:pattern FLT.length (set_as_list s)}
FLT.length (set_as_list s) = cardinality s | {
"file_name": "ulib/FStar.FiniteSet.Base.fsti",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 46,
"end_line": 417,
"start_col": 0,
"start_line": 415
} | (*
Copyright 2008-2021 John Li, Jay Lorch, Rustan Leino, Alex Summers,
Dan Rosen, Nikhil Swamy, Microsoft Research, and contributors to
the Dafny Project
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.
Includes material from the Dafny project
(https://github.com/dafny-lang/dafny) which carries this license
information:
Created 9 February 2008 by Rustan Leino.
Converted to Boogie 2 on 28 June 2008.
Edited sequence axioms 20 October 2009 by Alex Summers.
Modified 2014 by Dan Rosen.
Copyright (c) 2008-2014, Microsoft.
Copyright by the contributors to the Dafny Project
SPDX-License-Identifier: MIT
*)
(**
This module declares a type and functions used for modeling
finite sets as they're modeled in Dafny.
@summary Type and functions for modeling finite sets
*)
module FStar.FiniteSet.Base
open FStar.FunctionalExtensionality
module FLT = FStar.List.Tot
val set (a: eqtype)
: Type0
(**
We translate each Dafny sequence function prefixed with `Set#`
into an F* function.
**)
/// We represent the Dafny operator [] on sets with `mem`:
val mem (#a: eqtype) (x: a) (s: set a)
: bool
/// We can convert a set to a list with `set_as_list`:
let rec list_nonrepeating (#a: eqtype) (xs: list a) : bool =
match xs with
| [] -> true
| hd :: tl -> not (FLT.mem hd tl) && list_nonrepeating tl
val set_as_list (#a: eqtype) (s: set a)
: GTot (xs: list a{list_nonrepeating xs /\ (forall x. FLT.mem x xs = mem x s)})
/// We represent the Dafny function `Set#Card` with `cardinality`:
///
/// function Set#Card<T>(Set T): int;
val cardinality (#a: eqtype) (s: set a)
: GTot nat
/// We represent the Dafny function `Set#Empty` with `empty`:
///
/// function Set#Empty<T>(): Set T;
val emptyset (#a: eqtype)
: set a
/// We represent the Dafny function `Set#UnionOne` with `insert`:
///
/// function Set#UnionOne<T>(Set T, T): Set T;
val insert (#a: eqtype) (x: a) (s: set a)
: set a
/// We represent the Dafny function `Set#Singleton` with `singleton`:
///
/// function Set#Singleton<T>(T): Set T;
val singleton (#a: eqtype) (x: a)
: set a
/// We represent the Dafny function `Set#Union` with `union`:
///
/// function Set#Union<T>(Set T, Set T): Set T;
val union (#a: eqtype) (s1: set a) (s2: set a)
: (set a)
/// We represent the Dafny function `Set#Intersection` with `intersection`:
///
/// function Set#Intersection<T>(Set T, Set T): Set T;
val intersection (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Difference` with `difference`:
///
/// function Set#Difference<T>(Set T, Set T): Set T;
val difference (#a: eqtype) (s1: set a) (s2: set a)
: set a
/// We represent the Dafny function `Set#Subset` with `subset`:
///
/// function Set#Subset<T>(Set T, Set T): bool;
val subset (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Equal` with `equal`:
///
/// function Set#Equal<T>(Set T, Set T): bool;
val equal (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny function `Set#Disjoint` with `disjoint`:
///
/// function Set#Disjoint<T>(Set T, Set T): bool;
val disjoint (#a: eqtype) (s1: set a) (s2: set a)
: Type0
/// We represent the Dafny choice operator by `choose`:
///
/// var x: T :| x in s;
val choose (#a: eqtype) (s: set a{exists x. mem x s})
: GTot (x: a{mem x s})
/// We add the utility functions `remove` and `notin`:
let remove (#a: eqtype) (x: a) (s: set a)
: set a =
difference s (singleton x)
let notin (#a: eqtype) (x: a) (s: set a)
: bool =
not (mem x s)
(**
We translate each finite set axiom from the Dafny prelude into an F*
predicate ending in `_fact`.
**)
/// We don't need the following axiom since we return a nat from cardinality:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) } 0 <= Set#Card(s));
/// We represent the following Dafny axiom with `empty_set_contains_no_elements_fact`:
///
/// axiom (forall<T> o: T :: { Set#Empty()[o] } !Set#Empty()[o]);
let empty_set_contains_no_elements_fact =
forall (a: eqtype) (o: a).{:pattern mem o (emptyset)} not (mem o (emptyset #a))
/// We represent the following Dafny axiom with `length_zero_fact`:
///
/// axiom (forall<T> s: Set T :: { Set#Card(s) }
/// (Set#Card(s) == 0 <==> s == Set#Empty()) &&
/// (Set#Card(s) != 0 ==> (exists x: T :: s[x])));
let length_zero_fact =
forall (a: eqtype) (s: set a).{:pattern cardinality s}
(cardinality s = 0 <==> s == emptyset)
/\ (cardinality s <> 0 <==> (exists x. mem x s))
/// We represent the following Dafny axiom with `singleton_contains_argument_fact`:
///
/// axiom (forall<T> r: T :: { Set#Singleton(r) } Set#Singleton(r)[r]);
let singleton_contains_argument_fact =
forall (a: eqtype) (r: a).{:pattern singleton r} mem r (singleton r)
/// We represent the following Dafny axiom with `singleton_contains_fact`:
///
/// axiom (forall<T> r: T, o: T :: { Set#Singleton(r)[o] } Set#Singleton(r)[o] <==> r == o);
let singleton_contains_fact =
forall (a: eqtype) (r: a) (o: a).{:pattern mem o (singleton r)} mem o (singleton r) <==> r == o
/// We represent the following Dafny axiom with `singleton_cardinality_fact`:
///
/// axiom (forall<T> r: T :: { Set#Card(Set#Singleton(r)) } Set#Card(Set#Singleton(r)) == 1);
let singleton_cardinality_fact =
forall (a: eqtype) (r: a).{:pattern cardinality (singleton r)} cardinality (singleton r) = 1
/// We represent the following Dafny axiom with `insert_fact`:
///
/// axiom (forall<T> a: Set T, x: T, o: T :: { Set#UnionOne(a,x)[o] }
/// Set#UnionOne(a,x)[o] <==> o == x || a[o]);
let insert_fact =
forall (a: eqtype) (s: set a) (x: a) (o: a).{:pattern mem o (insert x s)}
mem o (insert x s) <==> o == x \/ mem o s
/// We represent the following Dafny axiom with `insert_contains_argument_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#UnionOne(a, x) }
/// Set#UnionOne(a, x)[x]);
let insert_contains_argument_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern insert x s}
mem x (insert x s)
/// We represent the following Dafny axiom with `insert_contains_fact`:
///
/// axiom (forall<T> a: Set T, x: T, y: T :: { Set#UnionOne(a, x), a[y] }
/// a[y] ==> Set#UnionOne(a, x)[y]);
let insert_contains_fact =
forall (a: eqtype) (s: set a) (x: a) (y: a).{:pattern insert x s; mem y s}
mem y s ==> mem y (insert x s)
/// We represent the following Dafny axiom with `insert_member_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a));
let insert_member_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
mem x s ==> cardinality (insert x s) = cardinality s
/// We represent the following Dafny axiom with `insert_nonmember_cardinality_fact`:
///
/// axiom (forall<T> a: Set T, x: T :: { Set#Card(Set#UnionOne(a, x)) }
/// !a[x] ==> Set#Card(Set#UnionOne(a, x)) == Set#Card(a) + 1);
let insert_nonmember_cardinality_fact =
forall (a: eqtype) (s: set a) (x: a).{:pattern cardinality (insert x s)}
not (mem x s) ==> cardinality (insert x s) = cardinality s + 1
/// We represent the following Dafny axiom with `union_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Union(a,b)[o] }
/// Set#Union(a,b)[o] <==> a[o] || b[o]);
let union_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (union s1 s2)}
mem o (union s1 s2) <==> mem o s1 \/ mem o s2
/// We represent the following Dafny axiom with `union_contains_element_from_first_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// a[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_first_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s1}
mem y s1 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_contains_element_from_second_argument_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Union(a, b), a[y] }
/// b[y] ==> Set#Union(a, b)[y]);
let union_contains_element_from_second_argument_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern union s1 s2; mem y s2}
mem y s2 ==> mem y (union s1 s2)
/// We represent the following Dafny axiom with `union_of_disjoint_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, b) }
/// Set#Disjoint(a, b) ==>
/// Set#Difference(Set#Union(a, b), a) == b &&
/// Set#Difference(Set#Union(a, b), b) == a);
let union_of_disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 s2}
disjoint s1 s2 ==> difference (union s1 s2) s1 == s2 /\ difference (union s1 s2) s2 == s1
/// We represent the following Dafny axiom with `intersection_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Intersection(a,b)[o] }
/// Set#Intersection(a,b)[o] <==> a[o] && b[o]);
let intersection_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (intersection s1 s2)}
mem o (intersection s1 s2) <==> mem o s1 /\ mem o s2
/// We represent the following Dafny axiom with `union_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(Set#Union(a, b), b) }
/// Set#Union(Set#Union(a, b), b) == Set#Union(a, b));
let union_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union (union s1 s2) s2}
union (union s1 s2) s2 == union s1 s2
/// We represent the following Dafny axiom with `union_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Union(a, Set#Union(a, b)) }
/// Set#Union(a, Set#Union(a, b)) == Set#Union(a, b));
let union_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern union s1 (union s1 s2)}
union s1 (union s1 s2) == union s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_right_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(Set#Intersection(a, b), b) }
/// Set#Intersection(Set#Intersection(a, b), b) == Set#Intersection(a, b));
let intersection_idempotent_right_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection (intersection s1 s2) s2}
intersection (intersection s1 s2) s2 == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_idempotent_left_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Intersection(a, Set#Intersection(a, b)) }
/// Set#Intersection(a, Set#Intersection(a, b)) == Set#Intersection(a, b));
let intersection_idempotent_left_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern intersection s1 (intersection s1 s2)}
intersection s1 (intersection s1 s2) == intersection s1 s2
/// We represent the following Dafny axiom with `intersection_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T :: { Set#Card(Set#Union(a, b)) }{ Set#Card(Set#Intersection(a, b)) }
/// Set#Card(Set#Union(a, b)) + Set#Card(Set#Intersection(a, b)) == Set#Card(a) + Set#Card(b));
let intersection_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (intersection s1 s2)}
cardinality (union s1 s2) + cardinality (intersection s1 s2) = cardinality s1 + cardinality s2
/// We represent the following Dafny axiom with `difference_contains_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T, o: T :: { Set#Difference(a,b)[o] }
/// Set#Difference(a,b)[o] <==> a[o] && !b[o]);
let difference_contains_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (o: a).{:pattern mem o (difference s1 s2)}
mem o (difference s1 s2) <==> mem o s1 /\ not (mem o s2)
/// We represent the following Dafny axiom with `difference_doesnt_include_fact`:
///
/// axiom (forall<T> a, b: Set T, y: T :: { Set#Difference(a, b), b[y] }
/// b[y] ==> !Set#Difference(a, b)[y] );
let difference_doesnt_include_fact =
forall (a: eqtype) (s1: set a) (s2: set a) (y: a).{:pattern difference s1 s2; mem y s2}
mem y s2 ==> not (mem y (difference s1 s2))
/// We represent the following Dafny axiom with `difference_cardinality_fact`:
///
/// axiom (forall<T> a, b: Set T ::
/// { Set#Card(Set#Difference(a, b)) }
/// Set#Card(Set#Difference(a, b)) + Set#Card(Set#Difference(b, a))
/// + Set#Card(Set#Intersection(a, b))
/// == Set#Card(Set#Union(a, b)) &&
/// Set#Card(Set#Difference(a, b)) == Set#Card(a) - Set#Card(Set#Intersection(a, b)));
let difference_cardinality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern cardinality (difference s1 s2)}
cardinality (difference s1 s2) + cardinality (difference s2 s1) + cardinality (intersection s1 s2) = cardinality (union s1 s2)
/\ cardinality (difference s1 s2) = cardinality s1 - cardinality (intersection s1 s2)
/// We represent the following Dafny axiom with `subset_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Subset(a,b) }
/// Set#Subset(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] ==> b[o]));
let subset_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern subset s1 s2}
subset s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 ==> mem o s2)
/// We represent the following Dafny axiom with `equal_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) }
/// Set#Equal(a,b) <==> (forall o: T :: {a[o]} {b[o]} a[o] <==> b[o]));
let equal_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} mem o s1 <==> mem o s2)
/// We represent the following Dafny axiom with `equal_extensionality_fact`:
///
/// axiom(forall<T> a: Set T, b: Set T :: { Set#Equal(a,b) } // extensionality axiom for sets
/// Set#Equal(a,b) ==> a == b);
let equal_extensionality_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern equal s1 s2}
equal s1 s2 ==> s1 == s2
/// We represent the following Dafny axiom with `disjoint_fact`:
///
/// axiom (forall<T> a: Set T, b: Set T :: { Set#Disjoint(a,b) }
/// Set#Disjoint(a,b) <==> (forall o: T :: {a[o]} {b[o]} !a[o] || !b[o]));
let disjoint_fact =
forall (a: eqtype) (s1: set a) (s2: set a).{:pattern disjoint s1 s2}
disjoint s1 s2 <==> (forall o.{:pattern mem o s1 \/ mem o s2} not (mem o s1) \/ not (mem o s2))
/// We add a few more facts for the utility function `remove` and for `set_as_list`:
let insert_remove_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern insert x (remove x s)}
mem x s = true ==> insert x (remove x s) == s
let remove_insert_fact =
forall (a: eqtype) (x: a) (s: set a).{:pattern remove x (insert x s)}
mem x s = false ==> remove x (insert x s) == s | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.fst.checked",
"FStar.FunctionalExtensionality.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.FiniteSet.Base.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FLT"
},
{
"abbrev": false,
"full_module": "FStar.FunctionalExtensionality",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.FiniteSet",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.l_Forall",
"Prims.eqtype",
"FStar.FiniteSet.Base.set",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.List.Tot.Base.length",
"FStar.FiniteSet.Base.set_as_list",
"FStar.FiniteSet.Base.cardinality"
] | [] | false | false | false | true | true | let set_as_list_cardinality_fact =
| forall (a: eqtype) (s: set a). {:pattern FLT.length (set_as_list s)}
FLT.length (set_as_list s) = cardinality s | false |
|
Hacl.Spec.P256.PrecompTable.fsti | Hacl.Spec.P256.PrecompTable.point_eval_list | val point_eval_list : p: Hacl.Spec.P256.PrecompTable.point_list -> Spec.P256.PointOps.proj_point | let point_eval_list (p:point_list) =
let x = Seq.seq_of_list p <: lseq uint64 12 in
from_mont_point (as_point_nat_seq x) | {
"file_name": "code/ecdsap256/Hacl.Spec.P256.PrecompTable.fsti",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 38,
"end_line": 49,
"start_col": 0,
"start_line": 47
} | module Hacl.Spec.P256.PrecompTable
open FStar.Mul
open Lib.IntTypes
open Lib.Sequence
open Hacl.Impl.P256.Point
module S = Spec.P256
module SM = Hacl.Spec.P256.Montgomery
module FL = FStar.List.Tot
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
unfold
let create4 (x0 x1 x2 x3:uint64) : list uint64 = [x0; x1; x2; x3]
inline_for_extraction noextract
let felem_list = x:list uint64{FL.length x == 4}
inline_for_extraction noextract
let point_list = x:list uint64{FL.length x == 12}
inline_for_extraction noextract
let felem_to_list (x:S.felem) : felem_list =
[@inline_let] let x0 = x % pow2 64 in
[@inline_let] let x1 = x / pow2 64 % pow2 64 in
[@inline_let] let x2 = x / pow2 128 % pow2 64 in
[@inline_let] let x3 = x / pow2 192 % pow2 64 in
[@inline_let] let r = create4 (u64 x0) (u64 x1) (u64 x2) (u64 x3) in
assert_norm (FL.length r = 4);
r
inline_for_extraction noextract
let proj_point_to_list (p:S.proj_point) : point_list =
[@inline_let] let (px, py, pz) = p in
[@inline_let] let pxM = SM.to_mont px in
[@inline_let] let pyM = SM.to_mont py in
[@inline_let] let pzM = SM.to_mont pz in
FL.(felem_to_list pxM @ felem_to_list pyM @ felem_to_list pzM)
inline_for_extraction noextract
let point_inv_list (p:point_list) =
let x = Seq.seq_of_list p <: lseq uint64 12 in
point_inv_seq x | {
"checked_file": "/",
"dependencies": [
"Spec.P256.fst.checked",
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Hacl.Spec.P256.Montgomery.fsti.checked",
"Hacl.Impl.P256.Point.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.P256.PrecompTable.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.List.Tot",
"short_module": "FL"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.P256.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Spec.P256",
"short_module": "S"
},
{
"abbrev": false,
"full_module": "Hacl.Impl.P256.Point",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Sequence",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.P256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Spec.P256.PrecompTable.point_list -> Spec.P256.PointOps.proj_point | Prims.Tot | [
"total"
] | [] | [
"Hacl.Spec.P256.PrecompTable.point_list",
"Hacl.Impl.P256.Point.from_mont_point",
"Hacl.Impl.P256.Point.as_point_nat_seq",
"Lib.Sequence.lseq",
"Lib.IntTypes.int_t",
"Lib.IntTypes.U64",
"Lib.IntTypes.SEC",
"FStar.Seq.Base.seq_of_list",
"Lib.IntTypes.uint64",
"Spec.P256.PointOps.proj_point"
] | [] | false | false | false | true | false | let point_eval_list (p: point_list) =
| let x = Seq.seq_of_list p <: lseq uint64 12 in
from_mont_point (as_point_nat_seq x) | false |
|
LowParse.TestLib.SLow.fst | LowParse.TestLib.SLow.test_string | val test_string (t: test_t) (testname inputstring: string)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) | val test_string (t: test_t) (testname inputstring: string)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) | let test_string (t:test_t) (testname:string) (inputstring:string): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
let input = bytes_of_hex inputstring in
test_bytes t testname input;
pop_frame() | {
"file_name": "src/lowparse/LowParse.TestLib.SLow.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 13,
"end_line": 48,
"start_col": 0,
"start_line": 44
} | module LowParse.TestLib.SLow
open FStar.HyperStack.ST
open FStar.Bytes
open FStar.HyperStack.IO
open FStar.Printf
module U32 = FStar.UInt32
#reset-options "--using_facts_from '* -LowParse'"
#reset-options "--z3cliopt smt.arith.nl=false"
(** The type of a unit test. It takes an input Bytes, parses it,
and returns a newly formatted Bytes. Or it returns None if
there is a fail to parse. *)
type test_t = (input:FStar.Bytes.bytes) -> Stack (option (FStar.Bytes.bytes * U32.t)) (fun _ -> true) (fun _ _ _ -> true)
assume val load_file: (filename:string) -> Tot FStar.Bytes.bytes
(** Test one parser+formatter pair against an in-memory buffer of Bytes *)
let test_bytes (t:test_t) (testname:string) (input:FStar.Bytes.bytes): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
print_string (sprintf "==== Testing Bytes %s ====\n" testname);
let result = t input in
(match result with
| Some (formattedresult, offset) -> (
if U32.gt offset (len input) then (
print_string "Invalid length return - it is longer than the input buffer!"
) else (
let inputslice = FStar.Bytes.slice input 0ul offset in
if formattedresult = inputslice then
print_string "Formatted data matches original input data\n"
else (
print_string "FAIL: formatted data does not match original input data\n"
)
)
)
| _ -> print_string "Failed to parse\n"
);
print_string (sprintf "==== Finished %s ====\n" testname);
pop_frame() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.IO.fst.checked",
"FStar.Bytes.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.TestLib.SLow.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Printf",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.IO",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Bytes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: LowParse.TestLib.SLow.test_t -> testname: Prims.string -> inputstring: Prims.string
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"LowParse.TestLib.SLow.test_t",
"Prims.string",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"LowParse.TestLib.SLow.test_bytes",
"FStar.Bytes.bytes",
"FStar.Bytes.bytes_of_hex",
"FStar.HyperStack.ST.push_frame",
"FStar.Monotonic.HyperStack.mem",
"Prims.b2t"
] | [] | false | true | false | false | false | let test_string (t: test_t) (testname inputstring: string)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) =
| push_frame ();
let input = bytes_of_hex inputstring in
test_bytes t testname input;
pop_frame () | false |
LowParse.TestLib.SLow.fst | LowParse.TestLib.SLow.test_file | val test_file (t: test_t) (filename: string) : Stack unit (fun _ -> true) (fun _ _ _ -> true) | val test_file (t: test_t) (filename: string) : Stack unit (fun _ -> true) (fun _ _ _ -> true) | let test_file (t:test_t) (filename:string): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
let input = load_file filename in
test_bytes t filename input;
pop_frame() | {
"file_name": "src/lowparse/LowParse.TestLib.SLow.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 13,
"end_line": 55,
"start_col": 0,
"start_line": 51
} | module LowParse.TestLib.SLow
open FStar.HyperStack.ST
open FStar.Bytes
open FStar.HyperStack.IO
open FStar.Printf
module U32 = FStar.UInt32
#reset-options "--using_facts_from '* -LowParse'"
#reset-options "--z3cliopt smt.arith.nl=false"
(** The type of a unit test. It takes an input Bytes, parses it,
and returns a newly formatted Bytes. Or it returns None if
there is a fail to parse. *)
type test_t = (input:FStar.Bytes.bytes) -> Stack (option (FStar.Bytes.bytes * U32.t)) (fun _ -> true) (fun _ _ _ -> true)
assume val load_file: (filename:string) -> Tot FStar.Bytes.bytes
(** Test one parser+formatter pair against an in-memory buffer of Bytes *)
let test_bytes (t:test_t) (testname:string) (input:FStar.Bytes.bytes): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
print_string (sprintf "==== Testing Bytes %s ====\n" testname);
let result = t input in
(match result with
| Some (formattedresult, offset) -> (
if U32.gt offset (len input) then (
print_string "Invalid length return - it is longer than the input buffer!"
) else (
let inputslice = FStar.Bytes.slice input 0ul offset in
if formattedresult = inputslice then
print_string "Formatted data matches original input data\n"
else (
print_string "FAIL: formatted data does not match original input data\n"
)
)
)
| _ -> print_string "Failed to parse\n"
);
print_string (sprintf "==== Finished %s ====\n" testname);
pop_frame()
(** Test one parser+formatter pair against a string of hex bytes, as Bytes *)
let test_string (t:test_t) (testname:string) (inputstring:string): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
let input = bytes_of_hex inputstring in
test_bytes t testname input;
pop_frame() | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.IO.fst.checked",
"FStar.Bytes.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.TestLib.SLow.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Printf",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.IO",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Bytes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: LowParse.TestLib.SLow.test_t -> filename: Prims.string -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"LowParse.TestLib.SLow.test_t",
"Prims.string",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"LowParse.TestLib.SLow.test_bytes",
"FStar.Bytes.bytes",
"LowParse.TestLib.SLow.load_file",
"FStar.HyperStack.ST.push_frame",
"FStar.Monotonic.HyperStack.mem",
"Prims.b2t"
] | [] | false | true | false | false | false | let test_file (t: test_t) (filename: string) : Stack unit (fun _ -> true) (fun _ _ _ -> true) =
| push_frame ();
let input = load_file filename in
test_bytes t filename input;
pop_frame () | false |
LowParse.TestLib.SLow.fst | LowParse.TestLib.SLow.test_bytes | val test_bytes (t: test_t) (testname: string) (input: FStar.Bytes.bytes)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) | val test_bytes (t: test_t) (testname: string) (input: FStar.Bytes.bytes)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) | let test_bytes (t:test_t) (testname:string) (input:FStar.Bytes.bytes): Stack unit (fun _ -> true) (fun _ _ _ -> true) =
push_frame();
print_string (sprintf "==== Testing Bytes %s ====\n" testname);
let result = t input in
(match result with
| Some (formattedresult, offset) -> (
if U32.gt offset (len input) then (
print_string "Invalid length return - it is longer than the input buffer!"
) else (
let inputslice = FStar.Bytes.slice input 0ul offset in
if formattedresult = inputslice then
print_string "Formatted data matches original input data\n"
else (
print_string "FAIL: formatted data does not match original input data\n"
)
)
)
| _ -> print_string "Failed to parse\n"
);
print_string (sprintf "==== Finished %s ====\n" testname);
pop_frame() | {
"file_name": "src/lowparse/LowParse.TestLib.SLow.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 13,
"end_line": 41,
"start_col": 0,
"start_line": 21
} | module LowParse.TestLib.SLow
open FStar.HyperStack.ST
open FStar.Bytes
open FStar.HyperStack.IO
open FStar.Printf
module U32 = FStar.UInt32
#reset-options "--using_facts_from '* -LowParse'"
#reset-options "--z3cliopt smt.arith.nl=false"
(** The type of a unit test. It takes an input Bytes, parses it,
and returns a newly formatted Bytes. Or it returns None if
there is a fail to parse. *)
type test_t = (input:FStar.Bytes.bytes) -> Stack (option (FStar.Bytes.bytes * U32.t)) (fun _ -> true) (fun _ _ _ -> true)
assume val load_file: (filename:string) -> Tot FStar.Bytes.bytes | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.UInt32.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.IO.fst.checked",
"FStar.Bytes.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.TestLib.SLow.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": false,
"full_module": "FStar.Printf",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.IO",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Bytes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.TestLib",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [
"smt.arith.nl=false"
],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: LowParse.TestLib.SLow.test_t -> testname: Prims.string -> input: FStar.Bytes.bytes
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"LowParse.TestLib.SLow.test_t",
"Prims.string",
"FStar.Bytes.bytes",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"FStar.HyperStack.IO.print_string",
"FStar.Printf.sprintf",
"FStar.UInt32.t",
"FStar.UInt32.gt",
"FStar.Bytes.len",
"Prims.bool",
"Prims.op_Equality",
"Prims.eq2",
"FStar.Seq.Base.seq",
"FStar.UInt8.t",
"FStar.Bytes.reveal",
"FStar.Seq.Base.slice",
"FStar.UInt32.v",
"FStar.UInt32.uint_to_t",
"FStar.Bytes.slice",
"FStar.UInt32.__uint_to_t",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.tuple2",
"FStar.HyperStack.ST.push_frame",
"FStar.Monotonic.HyperStack.mem",
"Prims.b2t"
] | [] | false | true | false | false | false | let test_bytes (t: test_t) (testname: string) (input: FStar.Bytes.bytes)
: Stack unit (fun _ -> true) (fun _ _ _ -> true) =
| push_frame ();
print_string (sprintf "==== Testing Bytes %s ====\n" testname);
let result = t input in
(match result with
| Some (formattedresult, offset) ->
(if U32.gt offset (len input)
then (print_string "Invalid length return - it is longer than the input buffer!")
else
(let inputslice = FStar.Bytes.slice input 0ul offset in
if formattedresult = inputslice
then print_string "Formatted data matches original input data\n"
else (print_string "FAIL: formatted data does not match original input data\n")))
| _ -> print_string "Failed to parse\n");
print_string (sprintf "==== Finished %s ====\n" testname);
pop_frame () | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.parse_u8_kind | val parse_u8_kind:parser_kind | val parse_u8_kind:parser_kind | let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1 | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 67,
"end_line": 12,
"start_col": 0,
"start_line": 12
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.total_constant_size_parser_kind"
] | [] | false | false | false | true | false | let parse_u8_kind:parser_kind =
| total_constant_size_parser_kind 1 | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.parse_u8 | val parse_u8:parser parse_u8_kind U8.t | val parse_u8:parser parse_u8_kind U8.t | let parse_u8: parser parse_u8_kind U8.t = tot_parse_u8 | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 54,
"end_line": 16,
"start_col": 0,
"start_line": 16
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
inline_for_extraction
let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1
val tot_parse_u8: tot_parser parse_u8_kind U8.t | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.Spec.Base.parser LowParse.Spec.Int.parse_u8_kind FStar.UInt8.t | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Int.tot_parse_u8"
] | [] | false | false | false | true | false | let parse_u8:parser parse_u8_kind U8.t =
| tot_parse_u8 | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.parse_u32_kind | val parse_u32_kind:parser_kind | val parse_u32_kind:parser_kind | let parse_u32_kind : parser_kind =
total_constant_size_parser_kind 4 | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 76,
"start_col": 0,
"start_line": 75
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
inline_for_extraction
let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1
val tot_parse_u8: tot_parser parse_u8_kind U8.t
let parse_u8: parser parse_u8_kind U8.t = tot_parse_u8
val parse_u8_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U8.v v == E.be_to_n (Seq.slice b 0 1)
)))
val parse_u8_spec'
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
v == Seq.index b 0
)))
val serialize_u8 : serializer parse_u8
val serialize_u8_spec
(x: U8.t)
: Lemma
(serialize serialize_u8 x `Seq.equal` Seq.create 1 x)
let serialize_u8_spec'
(x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\
Seq.index s 0 == x)
= serialize_u8_spec x
inline_for_extraction
let parse_u16_kind : parser_kind =
total_constant_size_parser_kind 2
val parse_u16: parser parse_u16_kind U16.t
val parse_u16_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 2))
(ensures (
let pp = parse parse_u16 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U16.v v == E.be_to_n (Seq.slice b 0 2)
)))
val serialize_u16 : serializer parse_u16 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.total_constant_size_parser_kind"
] | [] | false | false | false | true | false | let parse_u32_kind:parser_kind =
| total_constant_size_parser_kind 4 | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.parse_u16_kind | val parse_u16_kind:parser_kind | val parse_u16_kind:parser_kind | let parse_u16_kind : parser_kind =
total_constant_size_parser_kind 2 | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 57,
"start_col": 0,
"start_line": 56
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
inline_for_extraction
let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1
val tot_parse_u8: tot_parser parse_u8_kind U8.t
let parse_u8: parser parse_u8_kind U8.t = tot_parse_u8
val parse_u8_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U8.v v == E.be_to_n (Seq.slice b 0 1)
)))
val parse_u8_spec'
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
v == Seq.index b 0
)))
val serialize_u8 : serializer parse_u8
val serialize_u8_spec
(x: U8.t)
: Lemma
(serialize serialize_u8 x `Seq.equal` Seq.create 1 x)
let serialize_u8_spec'
(x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\
Seq.index s 0 == x)
= serialize_u8_spec x | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.total_constant_size_parser_kind"
] | [] | false | false | false | true | false | let parse_u16_kind:parser_kind =
| total_constant_size_parser_kind 2 | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.parse_u64_kind | val parse_u64_kind:parser_kind | val parse_u64_kind:parser_kind | let parse_u64_kind : parser_kind =
total_constant_size_parser_kind 8 | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 35,
"end_line": 95,
"start_col": 0,
"start_line": 94
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
inline_for_extraction
let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1
val tot_parse_u8: tot_parser parse_u8_kind U8.t
let parse_u8: parser parse_u8_kind U8.t = tot_parse_u8
val parse_u8_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U8.v v == E.be_to_n (Seq.slice b 0 1)
)))
val parse_u8_spec'
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
v == Seq.index b 0
)))
val serialize_u8 : serializer parse_u8
val serialize_u8_spec
(x: U8.t)
: Lemma
(serialize serialize_u8 x `Seq.equal` Seq.create 1 x)
let serialize_u8_spec'
(x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\
Seq.index s 0 == x)
= serialize_u8_spec x
inline_for_extraction
let parse_u16_kind : parser_kind =
total_constant_size_parser_kind 2
val parse_u16: parser parse_u16_kind U16.t
val parse_u16_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 2))
(ensures (
let pp = parse parse_u16 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U16.v v == E.be_to_n (Seq.slice b 0 2)
)))
val serialize_u16 : serializer parse_u16
inline_for_extraction
let parse_u32_kind : parser_kind =
total_constant_size_parser_kind 4
val parse_u32: parser parse_u32_kind U32.t
val parse_u32_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 4))
(ensures (
let pp = parse parse_u32 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U32.v v == E.be_to_n (Seq.slice b 0 4)
)))
val serialize_u32 : serializer parse_u32 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | LowParse.Spec.Base.parser_kind | Prims.Tot | [
"total"
] | [] | [
"LowParse.Spec.Base.total_constant_size_parser_kind"
] | [] | false | false | false | true | false | let parse_u64_kind:parser_kind =
| total_constant_size_parser_kind 8 | false |
LowParse.Spec.Int.fsti | LowParse.Spec.Int.serialize_u8_spec' | val serialize_u8_spec' (x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\ Seq.index s 0 == x) | val serialize_u8_spec' (x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\ Seq.index s 0 == x) | let serialize_u8_spec'
(x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\
Seq.index s 0 == x)
= serialize_u8_spec x | {
"file_name": "src/lowparse/LowParse.Spec.Int.fsti",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 21,
"end_line": 53,
"start_col": 0,
"start_line": 47
} | module LowParse.Spec.Int
include LowParse.Spec.Base
module Seq = FStar.Seq
module E = FStar.Endianness
module U8 = FStar.UInt8
module U16 = FStar.UInt16
module U32 = FStar.UInt32
module U64 = FStar.UInt64
inline_for_extraction
let parse_u8_kind : parser_kind = total_constant_size_parser_kind 1
val tot_parse_u8: tot_parser parse_u8_kind U8.t
let parse_u8: parser parse_u8_kind U8.t = tot_parse_u8
val parse_u8_spec
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
U8.v v == E.be_to_n (Seq.slice b 0 1)
)))
val parse_u8_spec'
(b: bytes)
: Lemma
(requires (Seq.length b >= 1))
(ensures (
let pp = parse parse_u8 b in
Some? pp /\ (
let (Some (v, consumed)) = pp in
v == Seq.index b 0
)))
val serialize_u8 : serializer parse_u8
val serialize_u8_spec
(x: U8.t)
: Lemma
(serialize serialize_u8 x `Seq.equal` Seq.create 1 x) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowParse.Spec.Base.fsti.checked",
"FStar.UInt8.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt16.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Endianness.fsti.checked"
],
"interface_file": false,
"source_file": "LowParse.Spec.Int.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "FStar.UInt16",
"short_module": "U16"
},
{
"abbrev": true,
"full_module": "FStar.UInt8",
"short_module": "U8"
},
{
"abbrev": true,
"full_module": "FStar.Endianness",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": false,
"full_module": "LowParse.Spec.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.UInt8.t
-> FStar.Pervasives.Lemma
(ensures
(let s = LowParse.Spec.Base.serialize LowParse.Spec.Int.serialize_u8 x in
FStar.Seq.Base.length s == 1 /\ FStar.Seq.Base.index s 0 == x)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.UInt8.t",
"LowParse.Spec.Int.serialize_u8_spec",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"Prims.int",
"FStar.Seq.Base.length",
"LowParse.Bytes.byte",
"FStar.Seq.Base.index",
"LowParse.Bytes.bytes",
"LowParse.Spec.Base.serialize",
"LowParse.Spec.Int.parse_u8_kind",
"LowParse.Spec.Int.parse_u8",
"LowParse.Spec.Int.serialize_u8",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let serialize_u8_spec' (x: U8.t)
: Lemma
(let s = serialize serialize_u8 x in
Seq.length s == 1 /\ Seq.index s 0 == x) =
| serialize_u8_spec x | false |
Pulse.Checker.Exists.fst | Pulse.Checker.Exists.terms_to_string | val terms_to_string (t: list term) : T.Tac string | val terms_to_string (t: list term) : T.Tac string | let terms_to_string (t:list term)
: T.Tac string
= String.concat "\n" (T.map Pulse.Syntax.Printer.term_to_string t) | {
"file_name": "lib/steel/pulse/Pulse.Checker.Exists.fst",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 68,
"end_line": 41,
"start_col": 0,
"start_line": 39
} | (*
Copyright 2023 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 Pulse.Checker.Exists
open Pulse.Syntax
open Pulse.Typing
open Pulse.Typing.Combinators
open Pulse.Checker.Pure
open Pulse.Checker.Base
open Pulse.Checker.Prover
module T = FStar.Tactics.V2
module P = Pulse.Syntax.Printer
module FV = Pulse.Typing.FV
module Metatheory = Pulse.Typing.Metatheory
let vprop_as_list_typing (#g:env) (#p:term)
(t:tot_typing g p tm_vprop)
(x:term { List.Tot.memP x (vprop_as_list p) })
: tot_typing g x tm_vprop
= assume false; t | {
"checked_file": "/",
"dependencies": [
"Pulse.Typing.Metatheory.fsti.checked",
"Pulse.Typing.FV.fsti.checked",
"Pulse.Typing.Env.fsti.checked",
"Pulse.Typing.Combinators.fsti.checked",
"Pulse.Typing.fst.checked",
"Pulse.Syntax.Printer.fsti.checked",
"Pulse.Syntax.fst.checked",
"Pulse.Checker.Pure.fsti.checked",
"Pulse.Checker.Prover.fsti.checked",
"Pulse.Checker.Base.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.String.fsti.checked",
"FStar.Printf.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked"
],
"interface_file": true,
"source_file": "Pulse.Checker.Exists.fst"
} | [
{
"abbrev": true,
"full_module": "Pulse.Typing.Metatheory",
"short_module": "Metatheory"
},
{
"abbrev": true,
"full_module": "Pulse.Typing.FV",
"short_module": "FV"
},
{
"abbrev": true,
"full_module": "Pulse.Syntax.Printer",
"short_module": "P"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "Pulse.Checker.Prover",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Checker.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Checker.Pure",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Typing.Combinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Typing",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Syntax",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Checker.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Typing",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Syntax",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "Pulse.Checker",
"short_module": null
},
{
"abbrev": false,
"full_module": "Pulse.Checker",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Prims.list Pulse.Syntax.Base.term -> FStar.Tactics.Effect.Tac Prims.string | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Pulse.Syntax.Base.term",
"FStar.String.concat",
"Prims.string",
"FStar.Tactics.Util.map",
"Pulse.Syntax.Printer.term_to_string"
] | [] | false | true | false | false | false | let terms_to_string (t: list term) : T.Tac string =
| String.concat "\n" (T.map Pulse.Syntax.Printer.term_to_string t) | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.mk_felem_zero | val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero) | val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero) | let mk_felem_zero b =
Hacl.Bignum25519.make_zero b | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 30,
"end_line": 37,
"start_col": 0,
"start_line": 36
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Hacl.Impl.Ed25519.Field51.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.felem",
"Hacl.Bignum25519.make_zero",
"Prims.unit"
] | [] | false | true | false | false | false | let mk_felem_zero b =
| Hacl.Bignum25519.make_zero b | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.mk_point_at_inf | val mk_point_at_inf: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_point_at_infinity) | val mk_point_at_inf: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_point_at_infinity) | let mk_point_at_inf p =
Hacl.Impl.Ed25519.PointConstants.make_point_inf p | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 51,
"end_line": 236,
"start_col": 0,
"start_line": 235
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one)
let mk_felem_one b =
Hacl.Bignum25519.make_one b
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fadd (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_add a b out =
Hacl.Impl.Curve25519.Field51.fadd out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fsub (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_sub a b out =
Hacl.Impl.Curve25519.Field51.fsub out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_mul a b out =
push_frame();
let tmp = create 10ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fmul out a b tmp;
pop_frame()
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 a))
let felem_sqr a out =
push_frame();
let tmp = create 5ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fsqr out a tmp;
pop_frame()
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2))
let felem_inv a out =
Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Load a little-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint
NOTE that the function also performs the reduction modulo 2^255."]
val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255)
let felem_load b out =
Hacl.Bignum25519.load_51 out b
[@@ Comment "Serialize a field element into little-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F51.felem -> out:lbuffer uint8 32ul ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == Lib.ByteSequence.nat_to_bytes_le 32 (F51.fevalh h0 a))
let felem_store a out =
Hacl.Bignum25519.store_51 out a
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the edwards25519 elliptic curve of the form
−x^2 + y^2 = 1 − (121665/121666) * x^2 * y^2.
This is a 64-bit optimized version, where a group element in extended homogeneous
coordinates (X, Y, Z, T) is represented as an array of 20 unsigned 64-bit
integers, i.e., uint64_t[20].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 20 limbs in size, i.e., uint64_t[20]."]
val mk_point_at_inf: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_point_at_infinity) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.Ed25519.Field51.point -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.point",
"Hacl.Impl.Ed25519.PointConstants.make_point_inf",
"Prims.unit"
] | [] | false | true | false | false | false | let mk_point_at_inf p =
| Hacl.Impl.Ed25519.PointConstants.make_point_inf p | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.mk_felem_one | val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one) | val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one) | let mk_felem_one b =
Hacl.Bignum25519.make_one b | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 29,
"end_line": 51,
"start_col": 0,
"start_line": 50
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Hacl.Impl.Ed25519.Field51.felem -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.felem",
"Hacl.Bignum25519.make_one",
"Prims.unit"
] | [] | false | true | false | false | false | let mk_felem_one b =
| Hacl.Bignum25519.make_one b | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.mk_base_point | val mk_base_point: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_g) | val mk_base_point: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_g) | let mk_base_point p =
Spec.Ed25519.Lemmas.g_is_on_curve ();
Hacl.Impl.Ed25519.PointConstants.make_g p | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 43,
"end_line": 251,
"start_col": 0,
"start_line": 249
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one)
let mk_felem_one b =
Hacl.Bignum25519.make_one b
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fadd (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_add a b out =
Hacl.Impl.Curve25519.Field51.fadd out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fsub (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_sub a b out =
Hacl.Impl.Curve25519.Field51.fsub out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_mul a b out =
push_frame();
let tmp = create 10ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fmul out a b tmp;
pop_frame()
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 a))
let felem_sqr a out =
push_frame();
let tmp = create 5ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fsqr out a tmp;
pop_frame()
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2))
let felem_inv a out =
Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Load a little-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint
NOTE that the function also performs the reduction modulo 2^255."]
val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255)
let felem_load b out =
Hacl.Bignum25519.load_51 out b
[@@ Comment "Serialize a field element into little-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F51.felem -> out:lbuffer uint8 32ul ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == Lib.ByteSequence.nat_to_bytes_le 32 (F51.fevalh h0 a))
let felem_store a out =
Hacl.Bignum25519.store_51 out a
[@@ CPrologue
"/*******************************************************************************
Verified group operations for the edwards25519 elliptic curve of the form
−x^2 + y^2 = 1 − (121665/121666) * x^2 * y^2.
This is a 64-bit optimized version, where a group element in extended homogeneous
coordinates (X, Y, Z, T) is represented as an array of 20 unsigned 64-bit
integers, i.e., uint64_t[20].
*******************************************************************************/\n";
Comment "Write the point at infinity (additive identity) in `p`.
The outparam `p` is meant to be 20 limbs in size, i.e., uint64_t[20]."]
val mk_point_at_inf: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_point_at_infinity)
let mk_point_at_inf p =
Hacl.Impl.Ed25519.PointConstants.make_point_inf p
[@@ Comment "Write the base point (generator) in `p`.
The outparam `p` is meant to be 20 limbs in size, i.e., uint64_t[20]."]
val mk_base_point: p:F51.point ->
Stack unit
(requires fun h -> live h p)
(ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\
F51.point_inv_t h1 p /\ F51.inv_ext_point (as_seq h1 p) /\
SE.to_aff_point (F51.point_eval h1 p) == SE.aff_g) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Hacl.Impl.Ed25519.Field51.point -> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.point",
"Hacl.Impl.Ed25519.PointConstants.make_g",
"Prims.unit",
"Spec.Ed25519.Lemmas.g_is_on_curve"
] | [] | false | true | false | false | false | let mk_base_point p =
| Spec.Ed25519.Lemmas.g_is_on_curve ();
Hacl.Impl.Ed25519.PointConstants.make_g p | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.felem_load | val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255) | val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255) | let felem_load b out =
Hacl.Bignum25519.load_51 out b | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 32,
"end_line": 192,
"start_col": 0,
"start_line": 191
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one)
let mk_felem_one b =
Hacl.Bignum25519.make_one b
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fadd (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_add a b out =
Hacl.Impl.Curve25519.Field51.fadd out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fsub (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_sub a b out =
Hacl.Impl.Curve25519.Field51.fsub out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_mul a b out =
push_frame();
let tmp = create 10ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fmul out a b tmp;
pop_frame()
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 a))
let felem_sqr a out =
push_frame();
let tmp = create 5ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fsqr out a tmp;
pop_frame()
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2))
let felem_inv a out =
Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Load a little-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint
NOTE that the function also performs the reduction modulo 2^255."]
val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: Lib.Buffer.lbuffer Lib.IntTypes.uint8 32ul -> out: Hacl.Impl.Ed25519.Field51.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"FStar.UInt32.__uint_to_t",
"Hacl.Impl.Ed25519.Field51.felem",
"Hacl.Bignum25519.load_51",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_load b out =
| Hacl.Bignum25519.load_51 out b | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.felem_store | val felem_store: a:F51.felem -> out:lbuffer uint8 32ul ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == Lib.ByteSequence.nat_to_bytes_le 32 (F51.fevalh h0 a)) | val felem_store: a:F51.felem -> out:lbuffer uint8 32ul ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == Lib.ByteSequence.nat_to_bytes_le 32 (F51.fevalh h0 a)) | let felem_store a out =
Hacl.Bignum25519.store_51 out a | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 33,
"end_line": 212,
"start_col": 0,
"start_line": 211
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one)
let mk_felem_one b =
Hacl.Bignum25519.make_one b
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fadd (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_add a b out =
Hacl.Impl.Curve25519.Field51.fadd out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fsub (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_sub a b out =
Hacl.Impl.Curve25519.Field51.fsub out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_mul a b out =
push_frame();
let tmp = create 10ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fmul out a b tmp;
pop_frame()
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 a))
let felem_sqr a out =
push_frame();
let tmp = create 5ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fsqr out a tmp;
pop_frame()
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2))
let felem_inv a out =
Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Load a little-endian field element from memory.
The argument `b` points to 32 bytes of valid memory, i.e., uint8_t[32].
The outparam `out` points to a field element of 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `b` and `out` are disjoint
NOTE that the function also performs the reduction modulo 2^255."]
val felem_load: b:lbuffer uint8 32ul -> out:F51.felem ->
Stack unit
(requires fun h -> live h b /\ live h out)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.as_nat h1 out == Lib.ByteSequence.nat_from_bytes_le (as_seq h0 b) % pow2 255)
let felem_load b out =
Hacl.Bignum25519.load_51 out b
[@@ Comment "Serialize a field element into little-endian memory.
The argument `a` points to a field element of 5 limbs in size, i.e., uint64_t[5].
The outparam `out` points to 32 bytes of valid memory, i.e., uint8_t[32].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_store: a:F51.felem -> out:lbuffer uint8 32ul ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
as_seq h1 out == Lib.ByteSequence.nat_to_bytes_le 32 (F51.fevalh h0 a)) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Impl.Ed25519.Field51.felem -> out: Lib.Buffer.lbuffer Lib.IntTypes.uint8 32ul
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.felem",
"Lib.Buffer.lbuffer",
"Lib.IntTypes.uint8",
"FStar.UInt32.__uint_to_t",
"Hacl.Bignum25519.store_51",
"Prims.unit"
] | [] | false | true | false | false | false | let felem_store a out =
| Hacl.Bignum25519.store_51 out a | false |
Hacl.EC.Ed25519.fst | Hacl.EC.Ed25519.felem_inv | val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2)) | val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2)) | let felem_inv a out =
Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out | {
"file_name": "code/ed25519/Hacl.EC.Ed25519.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 33,
"end_line": 171,
"start_col": 0,
"start_line": 169
} | module Hacl.EC.Ed25519
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
module ST = FStar.HyperStack.ST
module F51 = Hacl.Impl.Ed25519.Field51
module SE = Spec.Ed25519
module SC = Spec.Curve25519
module ML = Hacl.Impl.Ed25519.Ladder
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
[@@ CPrologue
"/*******************************************************************************
Verified field arithmetic modulo p = 2^255 - 19.
This is a 64-bit optimized version, where a field element in radix-2^{51} is
represented as an array of five unsigned 64-bit integers, i.e., uint64_t[5].
*******************************************************************************/\n";
Comment "Write the additive identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_zero: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.zero)
let mk_felem_zero b =
Hacl.Bignum25519.make_zero b
[@@ Comment "Write the multiplicative identity in `f`.
The outparam `f` is meant to be 5 limbs in size, i.e., uint64_t[5]."]
val mk_felem_one: b:F51.felem ->
Stack unit
(requires fun h -> live h b)
(ensures fun h0 _ h1 -> modifies (loc b) h0 h1 /\
F51.mul_inv_t h1 b /\
F51.fevalh h1 b == SC.one)
let mk_felem_one b =
Hacl.Bignum25519.make_one b
[@@ Comment "Write `a + b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_add: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fadd (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_add a b out =
Hacl.Impl.Curve25519.Field51.fadd out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a - b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_sub: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
(disjoint out a \/ out == a) /\
(disjoint out b \/ out == b) /\
(disjoint a b \/ a == b) /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fsub (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_sub a b out =
Hacl.Impl.Curve25519.Field51.fsub out a b;
Hacl.Bignum25519.reduce_513 out
[@@ Comment "Write `a * b mod p` in `out`.
The arguments `a`, `b`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a`, `b`, and `out` are either pairwise disjoint or equal"]
val felem_mul: a:F51.felem -> b:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h b /\ live h out /\
F51.mul_inv_t h a /\
F51.mul_inv_t h b)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 b))
let felem_mul a b out =
push_frame();
let tmp = create 10ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fmul out a b tmp;
pop_frame()
[@@ Comment "Write `a * a mod p` in `out`.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are either disjoint or equal"]
val felem_sqr: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fmul (F51.fevalh h0 a) (F51.fevalh h0 a))
let felem_sqr a out =
push_frame();
let tmp = create 5ul (u128 0) in
Hacl.Impl.Curve25519.Field51.fsqr out a tmp;
pop_frame()
[@@ Comment "Write `a ^ (p - 2) mod p` in `out`.
The function computes modular multiplicative inverse if `a` <> zero.
The argument `a`, and the outparam `out` are meant to be 5 limbs in size, i.e., uint64_t[5].
Before calling this function, the caller will need to ensure that the following
precondition is observed.
• `a` and `out` are disjoint"]
val felem_inv: a:F51.felem -> out:F51.felem ->
Stack unit
(requires fun h ->
live h a /\ live h out /\ disjoint a out /\
F51.mul_inv_t h a)
(ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\
F51.mul_inv_t h1 out /\
F51.fevalh h1 out == SC.fpow (F51.fevalh h0 a) (SC.prime - 2)) | {
"checked_file": "/",
"dependencies": [
"Spec.Ed25519.Lemmas.fsti.checked",
"Spec.Ed25519.fst.checked",
"Spec.Curve25519.fst.checked",
"prims.fst.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Impl.Ed25519.PointNegate.fst.checked",
"Hacl.Impl.Ed25519.PointEqual.fst.checked",
"Hacl.Impl.Ed25519.PointDouble.fst.checked",
"Hacl.Impl.Ed25519.PointDecompress.fst.checked",
"Hacl.Impl.Ed25519.PointConstants.fst.checked",
"Hacl.Impl.Ed25519.PointCompress.fst.checked",
"Hacl.Impl.Ed25519.PointAdd.fst.checked",
"Hacl.Impl.Ed25519.Ladder.fsti.checked",
"Hacl.Impl.Ed25519.Field51.fst.checked",
"Hacl.Impl.Curve25519.Field51.fst.checked",
"Hacl.Bignum25519.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "Hacl.EC.Ed25519.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Ladder",
"short_module": "ML"
},
{
"abbrev": true,
"full_module": "Spec.Curve25519",
"short_module": "SC"
},
{
"abbrev": true,
"full_module": "Spec.Ed25519",
"short_module": "SE"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Ed25519.Field51",
"short_module": "F51"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.EC",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Impl.Ed25519.Field51.felem -> out: Hacl.Impl.Ed25519.Field51.felem
-> FStar.HyperStack.ST.Stack Prims.unit | FStar.HyperStack.ST.Stack | [] | [] | [
"Hacl.Impl.Ed25519.Field51.felem",
"Hacl.Bignum25519.reduce_513",
"Prims.unit",
"Hacl.Bignum25519.inverse"
] | [] | false | true | false | false | false | let felem_inv a out =
| Hacl.Bignum25519.inverse out a;
Hacl.Bignum25519.reduce_513 out | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.