effect
stringclasses 48
values | original_source_type
stringlengths 0
23k
| opens_and_abbrevs
listlengths 2
92
| isa_cross_project_example
bool 1
class | source_definition
stringlengths 9
57.9k
| partial_definition
stringlengths 7
23.3k
| is_div
bool 2
classes | is_type
null | is_proof
bool 2
classes | completed_definiton
stringlengths 1
250k
| dependencies
dict | effect_flags
sequencelengths 0
2
| ideal_premises
sequencelengths 0
236
| mutual_with
sequencelengths 0
11
| file_context
stringlengths 0
407k
| interleaved
bool 1
class | is_simply_typed
bool 2
classes | file_name
stringlengths 5
48
| vconfig
dict | is_simple_lemma
null | source_type
stringlengths 10
23k
| proof_features
sequencelengths 0
1
| name
stringlengths 8
95
| source
dict | verbose_type
stringlengths 1
7.42k
| source_range
dict |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | 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)) | 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 = | false | null | true | 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)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.pos",
"FStar.BitVector.bv_t",
"FStar.Seq.Base.lemma_eq_intro",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.BitVector.logxor_vec",
"Prims.op_Addition",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.logxor_vec_append | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
a1: FStar.BitVector.bv_t n1 ->
b1: FStar.BitVector.bv_t n1 ->
a2: FStar.BitVector.bv_t n2 ->
b2: FStar.BitVector.bv_t n2
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.append (FStar.BitVector.logxor_vec a1 b1) (FStar.BitVector.logxor_vec a2 b2) ==
FStar.BitVector.logxor_vec (FStar.Seq.Base.append a1 a2) (FStar.Seq.Base.append b1 b2)) | {
"end_col": 85,
"end_line": 421,
"start_col": 2,
"start_line": 420
} |
Prims.Pure | val mul_wide: x:U64.t -> y:U64.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U64.v y)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | 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 | 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)) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"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"
] | [] | (*
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
let mul_wide (x y:U64.t) : Pure t
(requires True) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_wide: x:U64.t -> y:U64.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U64.v y)) | [] | FStar.UInt128.mul_wide | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 19,
"end_line": 1214,
"start_col": 2,
"start_line": 1210
} |
Prims.Pure | val 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))) | [
{
"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
}
] | false | 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 | val 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 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))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"FStar.UInt64.add",
"Prims.unit",
"FStar.UInt128.mod_mul_pow2",
"FStar.UInt64.v",
"Prims.op_Subtraction",
"Prims._assert",
"Prims.pow2",
"FStar.UInt128.pow2_div_bound",
"FStar.UInt64.n",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.mod_mul",
"FStar.Math.Lemmas.pow2_plus",
"Prims.op_Division",
"FStar.Mul.op_Star",
"Prims.op_Modulus",
"FStar.UInt.uint_t",
"FStar.UInt64.shift_right",
"FStar.UInt32.sub",
"FStar.UInt128.u32_64",
"FStar.UInt64.shift_left",
"Prims.op_disEquality",
"Prims.op_Equality",
"Prims.op_Addition"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "native",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 40,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | FStar.UInt128.add_u64_shift_left | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64}
-> Prims.Pure FStar.UInt64.t | {
"end_col": 18,
"end_line": 558,
"start_col": 105,
"start_line": 545
} |
Prims.Pure | val 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))) | [
{
"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
}
] | false | 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 | val 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 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))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt32.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt32.v",
"Prims.op_LessThan",
"Prims.unit",
"FStar.UInt128.div_plus_multiple",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.pow2",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Division",
"FStar.UInt128.v",
"Prims.op_Subtraction",
"FStar.UInt128.div_product",
"FStar.Math.Lemmas.pow2_plus",
"FStar.UInt.uint_t",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.shift_right",
"FStar.UInt32.sub",
"FStar.UInt128.u32_64",
"FStar.UInt64.uint_to_t",
"Prims.l_True"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | FStar.UInt128.shift_right_large | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> s: FStar.UInt32.t{FStar.UInt32.v s >= 64 /\ FStar.UInt32.v s < 128}
-> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 798,
"start_col": 52,
"start_line": 790
} |
FStar.Pervasives.Lemma | val product_div_bound (#n: pos) (x y: UInt.uint_t n) : Lemma (x * y / pow2 n < pow2 n) | [
{
"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
}
] | 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 | 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) = | false | null | true | Math.pow2_plus n n;
product_bound x y (pow2 n);
pow2_div_bound #(n + n) (x * y) n | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val product_div_bound (#n: pos) (x y: UInt.uint_t n) : Lemma (x * y / pow2 n < pow2 n) | [] | FStar.UInt128.product_div_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt.uint_t n -> y: FStar.UInt.uint_t n
-> FStar.Pervasives.Lemma (ensures x * y / Prims.pow2 n < Prims.pow2 n) | {
"end_col": 33,
"end_line": 1205,
"start_col": 2,
"start_line": 1203
} |
Prims.Tot | 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)}) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | 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; } | 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))) = | false | null | false | 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 } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)}) | [] | FStar.UInt128.gte_mask | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) } | {
"end_col": 30,
"end_line": 892,
"start_col": 87,
"start_line": 884
} |
Prims.Tot | val constant_time_carry (a b: U64.t) : Tot U64.t | [
{
"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
}
] | false | 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 | val constant_time_carry (a b: U64.t) : Tot U64.t
let constant_time_carry (a b: U64.t) : Tot U64.t = | false | null | false | let open U64 in a ^^ ((a ^^ b) |^ ((a -%^ b) ^^ b)) >>^ 63ul | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt64.op_Greater_Greater_Hat",
"FStar.UInt64.op_Hat_Hat",
"FStar.UInt64.op_Bar_Hat",
"FStar.UInt64.op_Subtraction_Percent_Hat",
"FStar.UInt32.__uint_to_t"
] | [] | (*
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 | false | true | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val constant_time_carry (a b: U64.t) : Tot U64.t | [] | FStar.UInt128.constant_time_carry | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> FStar.UInt64.t | {
"end_col": 46,
"end_line": 103,
"start_col": 2,
"start_line": 99
} |
Prims.Tot | val vec128 (a: t) : BV.bv_t 128 | [
{
"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
}
] | false | let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a) | val vec128 (a: t) : BV.bv_t 128
let vec128 (a: t) : BV.bv_t 128 = | false | null | false | UInt.to_vec #128 (v a) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt128.t",
"FStar.UInt.to_vec",
"FStar.UInt128.v",
"FStar.BitVector.bv_t"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val vec128 (a: t) : BV.bv_t 128 | [] | FStar.UInt128.vec128 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> FStar.BitVector.bv_t 128 | {
"end_col": 56,
"end_line": 388,
"start_col": 34,
"start_line": 388
} |
Prims.Tot | val sub_mod_impl (a b: t) : t | [
{
"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
}
] | false | 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); } | val sub_mod_impl (a b: t) : t
let sub_mod_impl (a b: t) : t = | false | null | false | 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) } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.sub_mod",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.carry",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt64.t"
] | [] | (*
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); } | false | true | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub_mod_impl (a b: t) : t | [] | FStar.UInt128.sub_mod_impl | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> FStar.UInt128.t | {
"end_col": 69,
"end_line": 302,
"start_col": 31,
"start_line": 299
} |
Prims.Tot | val vec64 (a: U64.t) : BV.bv_t 64 | [
{
"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
}
] | false | let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a) | val vec64 (a: U64.t) : BV.bv_t 64
let vec64 (a: U64.t) : BV.bv_t 64 = | false | null | false | UInt.to_vec (U64.v a) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt.to_vec",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.BitVector.bv_t"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val vec64 (a: U64.t) : BV.bv_t 64 | [] | FStar.UInt128.vec64 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> FStar.BitVector.bv_t 64 | {
"end_col": 57,
"end_line": 389,
"start_col": 36,
"start_line": 389
} |
Prims.Tot | [
{
"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
}
] | false | let fact0 (a b: uint_t 64) = carry_bv a b == int2bv 0 | let fact0 (a b: uint_t 64) = | false | null | false | carry_bv a b == int2bv 0 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt.uint_t",
"Prims.eq2",
"FStar.BV.bv_t",
"FStar.UInt128.carry_bv",
"FStar.BV.int2bv",
"Prims.logical"
] | [] | (*
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()) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val fact0 : a: FStar.UInt.uint_t 64 -> b: FStar.UInt.uint_t 64 -> Prims.logical | [] | FStar.UInt128.fact0 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt.uint_t 64 -> b: FStar.UInt.uint_t 64 -> Prims.logical | {
"end_col": 53,
"end_line": 68,
"start_col": 29,
"start_line": 68
} |
|
Prims.Tot | val t: (x:Type0{hasEq x}) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let t = uint128 | val t: (x:Type0{hasEq x})
let t = | false | null | false | uint128 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt128.uint128"
] | [] | (*
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 } | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val t: (x:Type0{hasEq x}) | [] | FStar.UInt128.t | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: Type0{Prims.hasEq x} | {
"end_col": 15,
"end_line": 113,
"start_col": 8,
"start_line": 113
} |
FStar.Pervasives.Lemma | val lem_ult (a b: uint_t 64) : Lemma (if a < b then fact1 a b else fact0 a b) | [
{
"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
}
] | false | 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 | val lem_ult (a b: uint_t 64) : Lemma (if a < b then fact1 a b else fact0 a b)
let lem_ult (a b: uint_t 64) : Lemma (if a < b then fact1 a b else fact0 a b) = | false | null | true | int2bv_ult a b;
lem_ult_1 a b;
lem_ult_2 a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt.uint_t",
"FStar.UInt128.lem_ult_2",
"Prims.unit",
"FStar.UInt128.lem_ult_1",
"FStar.UInt128.int2bv_ult",
"Prims.l_True",
"Prims.squash",
"Prims.op_LessThan",
"FStar.UInt128.fact1",
"Prims.bool",
"FStar.UInt128.fact0",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lem_ult (a b: uint_t 64) : Lemma (if a < b then fact1 a b else fact0 a b) | [] | FStar.UInt128.lem_ult | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt.uint_t 64 -> b: FStar.UInt.uint_t 64
-> FStar.Pervasives.Lemma
(ensures
((match a < b with
| true -> FStar.UInt128.fact1 a b
| _ -> FStar.UInt128.fact0 a b)
<:
Type0)) | {
"end_col": 17,
"end_line": 96,
"start_col": 4,
"start_line": 94
} |
Prims.Pure | val 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))) | [
{
"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
}
] | false | 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 | val 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)))
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))) = | false | null | false | constant_time_carry_ok a b;
constant_time_carry a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt128.constant_time_carry",
"Prims.unit",
"FStar.UInt128.constant_time_carry_ok",
"Prims.l_True",
"Prims.eq2",
"Prims.int",
"FStar.UInt64.v",
"Prims.op_LessThan",
"Prims.bool"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | FStar.UInt128.carry | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 25,
"end_line": 176,
"start_col": 2,
"start_line": 175
} |
Prims.Tot | val lem_ult_1 (a b: uint_t 64) : squash (bvult (int2bv a) (int2bv b) ==> fact1 a b) | [
{
"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
}
] | false | 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()) | val lem_ult_1 (a b: uint_t 64) : squash (bvult (int2bv a) (int2bv b) ==> fact1 a b)
let lem_ult_1 (a b: uint_t 64) : squash (bvult (int2bv a) (int2bv b) ==> fact1 a b) = | false | null | true | FStar.Tactics.Effect.assert_by_tactic (bvult (int2bv a) (int2bv b) ==> fact1 a b)
(fun _ ->
();
(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 ())) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt.uint_t",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.l_imp",
"Prims.b2t",
"FStar.BV.bvult",
"FStar.BV.int2bv",
"FStar.UInt128.fact1",
"Prims.unit",
"FStar.Tactics.V2.Derived.smt",
"FStar.Tactics.V2.Builtins.set_options",
"FStar.Tactics.V2.Builtins.norm",
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.delta_only",
"Prims.string",
"Prims.Nil",
"Prims.squash"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lem_ult_1 (a b: uint_t 64) : squash (bvult (int2bv a) (int2bv b) ==> fact1 a b) | [] | FStar.UInt128.lem_ult_1 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt.uint_t 64 -> b: FStar.UInt.uint_t 64
-> Prims.squash (FStar.BV.bvult (FStar.BV.int2bv a) (FStar.BV.int2bv b) ==>
FStar.UInt128.fact1 a b) | {
"end_col": 17,
"end_line": 75,
"start_col": 4,
"start_line": 72
} |
FStar.Pervasives.Lemma | val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k) | [
{
"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
}
] | false | let mod_mod a k k' =
assert (a % k < k);
assert (a % k < k' * k) | 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' = | false | null | true | assert (a % k < k);
assert (a % k < k' * k) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims._assert",
"Prims.op_LessThan",
"Prims.op_Modulus",
"FStar.Mul.op_Star",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} ->
Lemma ((a % k) % (k'*k) == a % k) | [] | FStar.UInt128.mod_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.nat -> k: Prims.nat{k > 0} -> k': Prims.nat{k' > 0}
-> FStar.Pervasives.Lemma (ensures a % k % (k' * k) == a % k) | {
"end_col": 25,
"end_line": 203,
"start_col": 2,
"start_line": 202
} |
Prims.Tot | val v (x:t) : Tot (uint_t n) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let v x = U64.v x.low + (U64.v x.high) * (pow2 64) | val v (x:t) : Tot (uint_t n)
let v x = | false | null | false | U64.v x.low + (U64.v x.high) * (pow2 64) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt128.t",
"Prims.op_Addition",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.Mul.op_Star",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.pow2",
"FStar.UInt.uint_t",
"FStar.UInt128.n"
] | [] | (*
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 | false | true | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val v (x:t) : Tot (uint_t n) | [] | FStar.UInt128.v | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt128.t -> FStar.UInt.uint_t FStar.UInt128.n | {
"end_col": 50,
"end_line": 119,
"start_col": 10,
"start_line": 119
} |
FStar.Pervasives.Lemma | val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2) | [
{
"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
}
] | false | let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2 | 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 = | false | null | true | Math.division_multiplication_lemma n m1 m2 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.Math.Lemmas.division_multiplication_lemma",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} ->
Lemma (n / (m1*m2) == (n / m1) / m2) | [] | FStar.UInt128.div_product | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> m1: Prims.nat{m1 > 0} -> m2: Prims.nat{m2 > 0}
-> FStar.Pervasives.Lemma (ensures n / (m1 * m2) == n / m1 / m2) | {
"end_col": 44,
"end_line": 211,
"start_col": 2,
"start_line": 211
} |
FStar.Pervasives.Lemma | val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n) | [
{
"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
}
] | false | let mul_div_cancel n k =
Math.cancel_mul_div n k | val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n)
let mul_div_cancel n k = | false | null | true | Math.cancel_mul_div n k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.Math.Lemmas.cancel_mul_div",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_div_cancel : n:nat -> k:nat{k>0} ->
Lemma ((n * k) / k == n) | [] | FStar.UInt128.mul_div_cancel | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n * k / k == n) | {
"end_col": 25,
"end_line": 216,
"start_col": 2,
"start_line": 216
} |
FStar.Pervasives.Lemma | val 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)) | [
{
"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
}
] | false | 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 | val 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))
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)) = | false | null | true | if U64.v a.low < U64.v b.low then sub_mod_wrap1_ok a b else sub_mod_wrap2_ok a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.op_LessThan",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.sub_mod_wrap1_ok",
"Prims.bool",
"FStar.UInt128.sub_mod_wrap2_ok",
"Prims.unit",
"Prims.b2t",
"Prims.op_Subtraction",
"FStar.UInt128.v",
"Prims.squash",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt128.sub_mod_impl",
"Prims.op_Addition",
"Prims.pow2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.sub_mod_wrap_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t
-> FStar.Pervasives.Lemma (requires FStar.UInt128.v a - FStar.UInt128.v b < 0)
(ensures
FStar.UInt128.v (FStar.UInt128.sub_mod_impl a b) =
FStar.UInt128.v a - FStar.UInt128.v b + Prims.pow2 128) | {
"end_col": 29,
"end_line": 359,
"start_col": 2,
"start_line": 357
} |
FStar.Pervasives.Lemma | val mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + n % k) | [
{
"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
}
] | false | let mod_spec_rew_n (n:nat) (k:nat{k > 0}) :
Lemma (n == n / k * k + n % k) = mod_spec n k | val mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + n % k)
let mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + n % k) = | false | null | true | mod_spec n k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.UInt128.mod_spec",
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + n % k) | [] | FStar.UInt128.mod_spec_rew_n | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n == (n / k) * k + n % k) | {
"end_col": 47,
"end_line": 224,
"start_col": 35,
"start_line": 224
} |
FStar.Pervasives.Lemma | val to_vec_v (a: t) : Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) | [
{
"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
}
] | false | 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 to_vec_v (a: t) : Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low))
let to_vec_v (a: t) : Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) = | false | null | true | to_vec_append (U64.v a.low) (U64.v a.high) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"FStar.UInt128.to_vec_append",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.Seq.Base.seq",
"Prims.bool",
"FStar.UInt128.vec128",
"FStar.Seq.Base.append",
"FStar.UInt128.vec64",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val to_vec_v (a: t) : Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low)) | [] | FStar.UInt128.to_vec_v | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt128.vec128 a ==
FStar.Seq.Base.append (FStar.UInt128.vec64 (Mkuint128?.high a))
(FStar.UInt128.vec64 (Mkuint128?.low a))) | {
"end_col": 44,
"end_line": 393,
"start_col": 2,
"start_line": 393
} |
Prims.Pure | val uint_to_t: x:uint_t n -> Pure t
(requires True)
(ensures (fun y -> v y = x)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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)); } | val uint_to_t: x:uint_t n -> Pure t
(requires True)
(ensures (fun y -> v y = x))
let uint_to_t x = | false | null | false | div_mod x (pow2 64);
{ low = U64.uint_to_t (x % (pow2 64)); high = U64.uint_to_t (x / (pow2 64)) } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt.uint_t",
"FStar.UInt128.n",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.uint_to_t",
"Prims.op_Modulus",
"Prims.pow2",
"Prims.op_Division",
"Prims.unit",
"FStar.UInt128.div_mod",
"FStar.UInt128.t"
] | [] | (*
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) = () | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val uint_to_t: x:uint_t n -> Pure t
(requires True)
(ensures (fun y -> v y = x)) | [] | FStar.UInt128.uint_to_t | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt.uint_t FStar.UInt128.n -> Prims.Pure FStar.UInt128.t | {
"end_col": 45,
"end_line": 126,
"start_col": 4,
"start_line": 124
} |
FStar.Pervasives.Lemma | val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2)) | [
{
"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
}
] | false | let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2 | val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2))
let mod_mul n k1 k2 = | false | null | true | Math.modulo_scale_lemma n k1 k2 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.modulo_scale_lemma",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_mul: n:nat -> k1:pos -> k2:pos ->
Lemma ((n % k2) * k1 == (n * k1) % (k1*k2)) | [] | FStar.UInt128.mod_mul | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k1: Prims.pos -> k2: Prims.pos
-> FStar.Pervasives.Lemma (ensures (n % k2) * k1 == n * k1 % (k1 * k2)) | {
"end_col": 33,
"end_line": 221,
"start_col": 2,
"start_line": 221
} |
FStar.Pervasives.Lemma | val shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0) | [
{
"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
}
] | false | 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 shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0)
let shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0) = | false | null | true | 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) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.UInt128.mod_mul_cancel",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Math.Lemmas.paren_mul_right",
"FStar.Math.Lemmas.pow2_plus",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Addition",
"Prims.l_True",
"Prims.squash",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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}) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0) | [] | FStar.UInt128.shift_past_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k1: Prims.nat -> k2: Prims.nat{k2 >= k1}
-> FStar.Pervasives.Lemma (ensures n * Prims.pow2 k2 % Prims.pow2 k1 == 0) | {
"end_col": 47,
"end_line": 488,
"start_col": 2,
"start_line": 485
} |
FStar.Pervasives.Lemma | val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n') | [
{
"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
}
] | false | 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 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' = | false | null | true | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"FStar.UInt.uint_t",
"FStar.Math.Lemmas.pow2_plus",
"Prims.unit",
"FStar.Math.Lemmas.distributivity_sub_left",
"Prims.pow2",
"FStar.Math.Lemmas.lemma_mult_le_right",
"Prims.op_Subtraction"
] | [] | (*
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') | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat ->
Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 n') | [] | FStar.UInt128.shift_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | num: FStar.UInt.uint_t n -> n': Prims.nat
-> FStar.Pervasives.Lemma (ensures num * Prims.pow2 n' <= Prims.pow2 (n' + n) - Prims.pow2 n') | {
"end_col": 21,
"end_line": 376,
"start_col": 2,
"start_line": 374
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | 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 | 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))
let sub_mod_wrap1_ok a b = | false | null | true | 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;
if U64.v a.high = U64.v b.high
then ()
else
(u64_diff_wrap a.high b.high;
()) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.op_Equality",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.bool",
"Prims.unit",
"FStar.UInt128.u64_diff_wrap",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.carry",
"FStar.UInt64.t",
"FStar.UInt64.sub_mod"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 10,
"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"
} | null | 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)) | [] | FStar.UInt128.sub_mod_wrap1_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t
-> FStar.Pervasives.Lemma
(requires
FStar.UInt128.v a - FStar.UInt128.v b < 0 /\
FStar.UInt64.v (Mkuint128?.low a) < FStar.UInt64.v (Mkuint128?.low b))
(ensures
FStar.UInt128.v (FStar.UInt128.sub_mod_impl a b) =
FStar.UInt128.v a - FStar.UInt128.v b + Prims.pow2 128) | {
"end_col": 7,
"end_line": 334,
"start_col": 26,
"start_line": 323
} |
FStar.Pervasives.Lemma | val mod_mul_cancel (n: nat) (k: nat{k > 0}) : Lemma ((n * k) % k == 0) | [
{
"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
}
] | false | 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;
() | val mod_mul_cancel (n: nat) (k: nat{k > 0}) : Lemma ((n * k) % k == 0)
let mod_mul_cancel (n: nat) (k: nat{k > 0}) : Lemma ((n * k) % k == 0) = | false | null | true | mod_spec (n * k) k;
mul_div_cancel n k;
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.unit",
"FStar.UInt128.mul_div_cancel",
"FStar.UInt128.mod_spec",
"FStar.Mul.op_Star",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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}) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_mul_cancel (n: nat) (k: nat{k > 0}) : Lemma ((n * k) % k == 0) | [] | FStar.UInt128.mod_mul_cancel | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n * k % k == 0) | {
"end_col": 4,
"end_line": 481,
"start_col": 2,
"start_line": 479
} |
Prims.Pure | val logand: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logand (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 logand: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logand (v a) (v b)))
let logand (a b: t) : Pure t (requires True) (ensures (fun r -> v r = UInt.logand #128 (v a) (v b))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.BitVector.bv_t",
"FStar.UInt128.vec128",
"FStar.BitVector.logand_vec",
"FStar.UInt128.to_vec_v",
"FStar.UInt128.logand_vec_append",
"FStar.UInt128.vec64",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.Seq.Base.seq",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.logand",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt.uint_t",
"FStar.UInt128.n",
"FStar.UInt128.v",
"FStar.UInt.logand"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val logand: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logand (v a) (v b))) | [] | FStar.UInt128.logand | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 414,
"start_col": 59,
"start_line": 404
} |
FStar.Pervasives.Lemma | val 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)) | [
{
"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
}
] | false | 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);
() | val 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))
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)) = | false | null | true | 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);
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.UInt64.v",
"FStar.UInt64.sub_mod",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_Addition",
"Prims.op_Subtraction",
"Prims.pow2",
"FStar.UInt128.sum_lt",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.Mul.op_Star",
"FStar.UInt128.carry",
"FStar.UInt128.sub_mod_impl",
"FStar.UInt64.t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt128.v",
"Prims.op_GreaterThanOrEqual",
"Prims.squash",
"Prims.op_Equality",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.sub_mod_wrap2_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t
-> FStar.Pervasives.Lemma
(requires
FStar.UInt128.v a - FStar.UInt128.v b < 0 /\
FStar.UInt64.v (Mkuint128?.low a) >= FStar.UInt64.v (Mkuint128?.low b))
(ensures
FStar.UInt128.v (FStar.UInt128.sub_mod_impl a b) =
FStar.UInt128.v a - FStar.UInt128.v b + Prims.pow2 128) | {
"end_col": 4,
"end_line": 352,
"start_col": 59,
"start_line": 343
} |
Prims.Pure | val lognot: a:t -> Pure t
(requires True)
(ensures (fun r -> v r == lognot (v a))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 | val lognot: a:t -> Pure t
(requires True)
(ensures (fun r -> v r == lognot (v a)))
let lognot (a: t) : Pure t (requires True) (ensures (fun r -> v r = UInt.lognot #128 (v a))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.BitVector.bv_t",
"FStar.UInt128.vec128",
"FStar.BitVector.lognot_vec",
"FStar.UInt128.to_vec_v",
"FStar.UInt128.lognot_vec_append",
"FStar.UInt128.vec64",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.Seq.Base.seq",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.lognot",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt.uint_t",
"FStar.UInt128.n",
"FStar.UInt128.v",
"FStar.UInt.lognot"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lognot: a:t -> Pure t
(requires True)
(ensures (fun r -> v r == lognot (v a))) | [] | FStar.UInt128.lognot | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 475,
"start_col": 53,
"start_line": 467
} |
Prims.Tot | val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2) | [
{
"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
}
] | false | let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1;
num1 + num2 * pow2 n1 | 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 = | false | null | false | shift_bound num2 n1;
num1 + num2 * pow2 n1 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"Prims.nat",
"FStar.UInt.uint_t",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.unit",
"FStar.UInt128.shift_bound"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2) | [] | FStar.UInt128.append_uint | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | num1: FStar.UInt.uint_t n1 -> num2: FStar.UInt.uint_t n2 -> FStar.UInt.uint_t (n1 + n2) | {
"end_col": 23,
"end_line": 381,
"start_col": 2,
"start_line": 380
} |
Prims.Pure | val add_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 add_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c))
let add_underspec (a b: t) = | false | null | false | let l = U64.add_mod a.low b.low in
if v a + v b < pow2 128 then carry_sum_ok a.low b.low;
{ low = l; high = U64.add_underspec (U64.add_underspec a.high b.high) (carry l b.low) } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.add_underspec",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.carry",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims.unit",
"Prims.op_LessThan",
"Prims.op_Addition",
"FStar.UInt128.v",
"Prims.pow2",
"FStar.UInt128.carry_sum_ok",
"Prims.bool",
"FStar.UInt64.t",
"FStar.UInt64.add_mod"
] | [] | (*
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); } | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a + v b) n ==> v a + v b = v c)) | [] | FStar.UInt128.add_underspec | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 83,
"end_line": 197,
"start_col": 28,
"start_line": 189
} |
FStar.Pervasives.Lemma | val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k) | [
{
"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
}
] | false | let mod_double a k =
mod_mod a k 1 | val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k)
let mod_double a k = | false | null | true | mod_mod a k 1 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.UInt128.mod_mod",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_double: a:nat -> k:nat{k>0} ->
Lemma (a % k % k == a % k) | [] | FStar.UInt128.mod_double | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures a % k % k == a % k) | {
"end_col": 15,
"end_line": 493,
"start_col": 2,
"start_line": 493
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | 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)) | 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 = | false | null | true | Seq.lemma_eq_intro (Seq.append (BV.lognot_vec a1) (BV.lognot_vec a2))
(BV.lognot_vec #(n1 + n2) (Seq.append a1 a2)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.pos",
"FStar.BitVector.bv_t",
"FStar.Seq.Base.lemma_eq_intro",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.BitVector.lognot_vec",
"Prims.op_Addition",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.lognot_vec_append | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a1: FStar.BitVector.bv_t n1 -> a2: FStar.BitVector.bv_t n2
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.append (FStar.BitVector.lognot_vec a1) (FStar.BitVector.lognot_vec a2) ==
FStar.BitVector.lognot_vec (FStar.Seq.Base.append a1 a2)) | {
"end_col": 67,
"end_line": 463,
"start_col": 2,
"start_line": 462
} |
Prims.Pure | val logor: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logor (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 logor: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logor (v a) (v b)))
let logor (a b: t) : Pure t (requires True) (ensures (fun r -> v r = UInt.logor #128 (v a) (v b))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.BitVector.bv_t",
"FStar.UInt128.vec128",
"FStar.BitVector.logor_vec",
"FStar.UInt128.to_vec_v",
"FStar.UInt128.logor_vec_append",
"FStar.UInt128.vec64",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.Seq.Base.seq",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.logor",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt.uint_t",
"FStar.UInt128.n",
"FStar.UInt128.v",
"FStar.UInt.logor"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val logor: a:t -> b:t -> Pure t
(requires True)
(ensures (fun r -> v r == logor (v a) (v b))) | [] | FStar.UInt128.logor | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 456,
"start_col": 58,
"start_line": 446
} |
Prims.Pure | val sub_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a - v b) % pow2 n = v c)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 | val sub_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a - v b) % pow2 n = v c))
let sub_mod (a b: t) : Pure t (requires True) (ensures (fun r -> v r = (v a - v b) % pow2 128)) = | false | null | false | (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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt128.sub_mod_impl",
"Prims.unit",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Subtraction",
"FStar.UInt128.v",
"FStar.UInt128.sub_mod_pos_ok",
"Prims.bool",
"FStar.UInt128.sub_mod_wrap_ok",
"Prims.l_True",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.pow2"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 40,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a - v b) % pow2 n = v c)) | [] | FStar.UInt128.sub_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 18,
"end_line": 368,
"start_col": 2,
"start_line": 365
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | 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)) | 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 = | false | null | true | 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)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.pos",
"FStar.BitVector.bv_t",
"FStar.Seq.Base.lemma_eq_intro",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.BitVector.logor_vec",
"Prims.op_Addition",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.logor_vec_append | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
a1: FStar.BitVector.bv_t n1 ->
b1: FStar.BitVector.bv_t n1 ->
a2: FStar.BitVector.bv_t n2 ->
b2: FStar.BitVector.bv_t n2
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.append (FStar.BitVector.logor_vec a1 b1) (FStar.BitVector.logor_vec a2 b2) ==
FStar.BitVector.logor_vec (FStar.Seq.Base.append a1 a2) (FStar.Seq.Base.append b1 b2)) | {
"end_col": 84,
"end_line": 442,
"start_col": 2,
"start_line": 441
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | 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)) | 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 = | false | null | true | 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)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.pos",
"FStar.BitVector.bv_t",
"FStar.Seq.Base.lemma_eq_intro",
"Prims.bool",
"FStar.Seq.Base.append",
"FStar.BitVector.logand_vec",
"Prims.op_Addition",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.logand_vec_append | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
a1: FStar.BitVector.bv_t n1 ->
b1: FStar.BitVector.bv_t n1 ->
a2: FStar.BitVector.bv_t n2 ->
b2: FStar.BitVector.bv_t n2
-> FStar.Pervasives.Lemma
(ensures
FStar.Seq.Base.append (FStar.BitVector.logand_vec a1 b1) (FStar.BitVector.logand_vec a2 b2) ==
FStar.BitVector.logand_vec (FStar.Seq.Base.append a1 a2) (FStar.Seq.Base.append b1 b2)) | {
"end_col": 85,
"end_line": 400,
"start_col": 2,
"start_line": 399
} |
FStar.Pervasives.Lemma | val div_plus_multiple (a b: nat) (k: pos)
: Lemma (requires (a < k)) (ensures ((a + b * k) / k == b)) | [
{
"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
}
] | false | 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_plus_multiple (a b: nat) (k: pos)
: Lemma (requires (a < k)) (ensures ((a + b * k) / k == b))
let div_plus_multiple (a b: nat) (k: pos)
: Lemma (requires (a < k)) (ensures ((a + b * k) / k == b)) = | false | null | true | Math.division_addition_lemma a k b;
Math.small_division_lemma_1 a k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"FStar.Math.Lemmas.small_division_lemma_1",
"Prims.unit",
"FStar.Math.Lemmas.division_addition_lemma",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Division",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val div_plus_multiple (a b: nat) (k: pos)
: Lemma (requires (a < k)) (ensures ((a + b * k) / k == b)) | [] | FStar.UInt128.div_plus_multiple | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.nat -> b: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (requires a < k) (ensures (a + b * k) / k == b) | {
"end_col": 33,
"end_line": 566,
"start_col": 2,
"start_line": 565
} |
Prims.Tot | val u32_64:n: U32.t{U32.v n == 64} | [
{
"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
}
] | false | let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64 | val u32_64:n: U32.t{U32.v n == 64}
let u32_64:n: U32.t{U32.v n == 64} = | false | null | false | U32.uint_to_t 64 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt32.uint_to_t"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u32_64:n: U32.t{U32.v n == 64} | [] | FStar.UInt128.u32_64 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: FStar.UInt32.t{FStar.UInt32.v n == 64} | {
"end_col": 61,
"end_line": 519,
"start_col": 45,
"start_line": 519
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | false | let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s | 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 = | false | null | true | shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.nat",
"FStar.UInt128.shift_left_large_lemma",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.unit"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.shift_left_large_lemma_t | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> s: Prims.nat
-> FStar.Pervasives.Lemma (requires s >= 64)
(ensures
FStar.UInt128.v a * Prims.pow2 s % Prims.pow2 128 ==
FStar.UInt64.v (Mkuint128?.low a) * Prims.pow2 s % Prims.pow2 128) | {
"end_col": 63,
"end_line": 517,
"start_col": 2,
"start_line": 517
} |
Prims.Pure | val sub: a:t -> b:t -> Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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); } | val sub: a:t -> b:t -> Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c))
let sub (a b: t) : Pure t (requires (v a - v b >= 0)) (ensures (fun r -> v r = v a - v b)) = | false | null | false | 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) } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.sub",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.carry",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt64.t",
"FStar.UInt64.sub_mod",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Subtraction",
"FStar.UInt128.v",
"Prims.op_Equality",
"Prims.int"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub: a:t -> b:t -> Pure t
(requires (size (v a - v b) n))
(ensures (fun c -> v a - v b = v c)) | [] | FStar.UInt128.sub | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 61,
"end_line": 291,
"start_col": 40,
"start_line": 288
} |
FStar.Pervasives.Lemma | val pow2_div_bound (#b: _) (n: UInt.uint_t b) (s: nat{s <= b}) : Lemma (n / pow2 s < pow2 (b - s)) | [
{
"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
}
] | false | 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 | val pow2_div_bound (#b: _) (n: UInt.uint_t b) (s: nat{s <= b}) : Lemma (n / pow2 s < pow2 (b - s))
let pow2_div_bound #b (n: UInt.uint_t b) (s: nat{s <= b}) : Lemma (n / pow2 s < pow2 (b - s)) = | false | null | true | Math.lemma_div_lt n b s | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.lemma_div_lt",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.op_LessThan",
"Prims.op_Division",
"Prims.pow2",
"Prims.op_Subtraction",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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}) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val pow2_div_bound (#b: _) (n: UInt.uint_t b) (s: nat{s <= b}) : Lemma (n / pow2 s < pow2 (b - s)) | [] | FStar.UInt128.pow2_div_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: FStar.UInt.uint_t b -> s: Prims.nat{s <= b}
-> FStar.Pervasives.Lemma (ensures n / Prims.pow2 s < Prims.pow2 (b - s)) | {
"end_col": 25,
"end_line": 540,
"start_col": 2,
"start_line": 540
} |
FStar.Pervasives.Lemma | val 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)) | [
{
"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
}
] | false | 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);
() | val 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))
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)) = | false | null | true | assert (sub a b == sub_mod_impl a b);
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"FStar.UInt128.sub",
"FStar.UInt128.sub_mod_impl",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_Subtraction",
"FStar.UInt128.v",
"Prims.squash",
"Prims.op_Equality",
"Prims.int",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 10,
"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": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.sub_mod_pos_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t
-> FStar.Pervasives.Lemma (requires FStar.UInt128.v a - FStar.UInt128.v b >= 0)
(ensures
FStar.UInt128.v (FStar.UInt128.sub_mod_impl a b) = FStar.UInt128.v a - FStar.UInt128.v b) | {
"end_col": 4,
"end_line": 309,
"start_col": 2,
"start_line": 308
} |
FStar.Pervasives.Lemma | val 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)) | [
{
"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
}
] | false | 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) | val 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))
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)) = | false | null | true | 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);
};
FStar.Tactics.Effect.assert_by_tactic (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))
(fun _ ->
();
(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) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt64.v_inj",
"FStar.UInt128.constant_time_carry",
"FStar.UInt64.lt",
"FStar.UInt64.uint_to_t",
"Prims.bool",
"Prims.unit",
"FStar.Tactics.Effect.assert_by_tactic",
"Prims.eq2",
"FStar.UInt.uint_t",
"FStar.UInt64.n",
"FStar.BV.bv2int",
"Prims.op_LessThan",
"FStar.UInt64.v",
"FStar.BV.int2bv",
"FStar.BV.bv_t",
"FStar.Tactics.V2.Builtins.norm",
"Prims.Nil",
"FStar.Pervasives.norm_step",
"FStar.Calc.calc_finish",
"Prims.Cons",
"FStar.Preorder.relation",
"FStar.Calc.calc_step",
"FStar.UInt128.carry_bv",
"FStar.UInt128.carry_uint64",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"FStar.UInt128.carry_uint64_equiv",
"Prims.squash",
"FStar.BV.inverse_num_lemma",
"FStar.UInt128.bv2int_fun",
"FStar.UInt128.carry_uint64_ok",
"FStar.UInt128.lem_ult",
"Prims.l_True",
"FStar.Pervasives.pattern"
] | [] | (*
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 == | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.constant_time_carry_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt128.constant_time_carry a b ==
(match FStar.UInt64.lt a b with
| true -> FStar.UInt64.uint_to_t 1
| _ -> FStar.UInt64.uint_to_t 0)) | {
"end_col": 98,
"end_line": 170,
"start_col": 4,
"start_line": 147
} |
Prims.Pure | val sub_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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); } | val sub_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c))
let sub_underspec (a b: t) = | false | null | false | 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) } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.sub_underspec",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.carry",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt64.t",
"FStar.UInt64.sub_mod"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sub_underspec: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c ->
size (v a - v b) n ==> v a - v b = v c)) | [] | FStar.UInt128.sub_underspec | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 81,
"end_line": 297,
"start_col": 28,
"start_line": 294
} |
FStar.Pervasives.Lemma | val shift_left_large_lemma
(#n1 #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)) | [
{
"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
}
] | false | 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));
() | val shift_left_large_lemma
(#n1 #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))
let shift_left_large_lemma
(#n1 #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)) = | false | null | true | 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));
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.unit",
"FStar.UInt128.mod_double",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.op_Addition",
"FStar.UInt128.shift_past_mod",
"FStar.UInt128.mod_add",
"FStar.UInt128.shift_left_large_val",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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) == | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 40,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_left_large_lemma
(#n1 #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)) | [] | FStar.UInt128.shift_left_large_lemma | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a1: FStar.UInt.uint_t n1 -> a2: FStar.UInt.uint_t n2 -> s: Prims.nat{s >= n2}
-> FStar.Pervasives.Lemma
(ensures
(a1 + a2 * Prims.pow2 n1) * Prims.pow2 s % Prims.pow2 (n1 + n2) ==
a1 * Prims.pow2 s % Prims.pow2 (n1 + n2)) | {
"end_col": 4,
"end_line": 509,
"start_col": 2,
"start_line": 505
} |
FStar.Pervasives.Lemma | val mod_then_mul_64 (n: nat) : Lemma ((n % pow2 64) * pow2 64 == n * pow2 64 % pow2 128) | [
{
"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
}
] | false | 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) | val mod_then_mul_64 (n: nat) : Lemma ((n % pow2 64) * pow2 64 == n * pow2 64 % pow2 128)
let mod_then_mul_64 (n: nat) : Lemma ((n % pow2 64) * pow2 64 == n * pow2 64 % pow2 128) = | false | null | true | Math.pow2_plus 64 64;
mod_mul n (pow2 64) (pow2 64) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"FStar.UInt128.mod_mul",
"Prims.pow2",
"Prims.unit",
"FStar.Math.Lemmas.pow2_plus",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"FStar.Mul.op_Star",
"Prims.op_Modulus",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_then_mul_64 (n: nat) : Lemma ((n % pow2 64) * pow2 64 == n * pow2 64 % pow2 128) | [] | FStar.UInt128.mod_then_mul_64 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat
-> FStar.Pervasives.Lemma
(ensures (n % Prims.pow2 64) * Prims.pow2 64 == n * Prims.pow2 64 % Prims.pow2 128) | {
"end_col": 31,
"end_line": 589,
"start_col": 2,
"start_line": 588
} |
FStar.Pervasives.Lemma | val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k)) | [
{
"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
}
] | false | 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 | 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 = | false | null | true | Math.lemma_mod_lt (n + m % k) k;
Math.modulo_lemma n k;
mod_add n m k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"FStar.UInt128.mod_add",
"Prims.unit",
"FStar.Math.Lemmas.modulo_lemma",
"FStar.Math.Lemmas.lemma_mod_lt",
"Prims.op_Addition",
"Prims.op_Modulus"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_mod_small' : n:nat -> m:nat -> k:pos ->
Lemma (requires (n + m % k < k))
(ensures (n + m % k == (n + m) % k)) | [] | FStar.UInt128.add_mod_small' | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (requires n + m % k < k) (ensures n + m % k == (n + m) % k) | {
"end_col": 15,
"end_line": 620,
"start_col": 2,
"start_line": 618
} |
Prims.Pure | val 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)) | [
{
"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
}
] | false | 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_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 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)) = | false | null | false | 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
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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.unit",
"FStar.UInt128.mul_abc_to_acb",
"Prims.pow2",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.Mul.op_Star",
"FStar.UInt64.v",
"Prims.op_Addition",
"Prims.op_Modulus",
"Prims.op_Division",
"Prims.op_Subtraction",
"FStar.UInt128.div_pow2_diff",
"FStar.UInt128.mod_then_mul_64",
"FStar.Math.Lemmas.distributivity_add_left",
"FStar.UInt.uint_t",
"FStar.UInt128.add_u64_shift_left",
"Prims.op_disEquality"
] | [] | (*
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 == | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.add_u64_shift_left_respec | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64}
-> Prims.Pure FStar.UInt64.t | {
"end_col": 3,
"end_line": 612,
"start_col": 63,
"start_line": 598
} |
FStar.Pervasives.Lemma | val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1) | [
{
"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
}
] | false | 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 | val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1)
let mul_mod_bound n s1 s2 = | false | null | true | mod_mul n (pow2 s1) (pow2 (s2 - 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"FStar.Math.Lemmas.pow2_plus",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.Math.Lemmas.lemma_mult_le_right",
"Prims.pow2",
"Prims.op_Modulus",
"FStar.Math.Lemmas.lemma_mod_lt",
"FStar.UInt128.mod_mul"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} ->
Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 s1) | [] | FStar.UInt128.mul_mod_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> s1: Prims.nat -> s2: Prims.nat{s2 >= s1}
-> FStar.Pervasives.Lemma
(ensures n * Prims.pow2 s1 % Prims.pow2 s2 <= Prims.pow2 s2 - Prims.pow2 s1) | {
"end_col": 27,
"end_line": 641,
"start_col": 2,
"start_line": 637
} |
Prims.Tot | val pll_l (x y: U64.t) : UInt.uint_t 32 | [
{
"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
}
] | false | let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y) | val pll_l (x y: U64.t) : UInt.uint_t 32
let pll_l (x y: U64.t) : UInt.uint_t 32 = | false | null | false | l32 (pll x y) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt128.l32",
"FStar.UInt128.pll",
"FStar.UInt.uint_t"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val pll_l (x y: U64.t) : UInt.uint_t 32 | [] | FStar.UInt128.pll_l | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t -> FStar.UInt.uint_t 32 | {
"end_col": 15,
"end_line": 986,
"start_col": 2,
"start_line": 986
} |
Prims.Tot | val pll_h (x y: U64.t) : UInt.uint_t 32 | [
{
"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
}
] | false | let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y) | val pll_h (x y: U64.t) : UInt.uint_t 32
let pll_h (x y: U64.t) : UInt.uint_t 32 = | false | null | false | h32 (pll x y) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt128.h32",
"FStar.UInt128.pll",
"FStar.UInt.uint_t"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val pll_h (x y: U64.t) : UInt.uint_t 32 | [] | FStar.UInt128.pll_h | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t -> FStar.UInt.uint_t 32 | {
"end_col": 15,
"end_line": 988,
"start_col": 2,
"start_line": 988
} |
FStar.Pervasives.Lemma | val 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)) | [
{
"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
}
] | false | 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;
() | val 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))
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)) = | false | null | true | Math.pow2_plus 64 s;
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt128.t",
"Prims.nat",
"Prims.unit",
"FStar.Math.Lemmas.pow2_plus",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.int",
"FStar.Mul.op_Star",
"FStar.UInt128.v",
"Prims.pow2",
"Prims.op_Addition",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | val 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)) | [] | FStar.UInt128.shift_t_val | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> s: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt128.v a * Prims.pow2 s ==
FStar.UInt64.v (Mkuint128?.low a) * Prims.pow2 s +
FStar.UInt64.v (Mkuint128?.high a) * Prims.pow2 (64 + s)) | {
"end_col": 6,
"end_line": 626,
"start_col": 4,
"start_line": 625
} |
Prims.Pure | val add_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a + v b) % pow2 n = v c)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 | val add_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a + v b) % pow2 n = v c))
let add_mod (a b: t) : Pure t (requires True) (ensures (fun r -> (v a + v b) % pow2 128 = v r)) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.unit",
"FStar.Pervasives.assert_spinoff",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"Prims.op_Modulus",
"Prims.op_Addition",
"FStar.UInt128.v",
"Prims.pow2",
"Prims._assert",
"Prims.eq2",
"FStar.Mul.op_Star",
"FStar.UInt128.mod_spec_rew_n",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_Division",
"FStar.UInt128.mod_add_small",
"FStar.Calc.calc_finish",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"FStar.Calc.calc_step",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Prims.squash",
"FStar.UInt128.mod_mul",
"FStar.Math.Lemmas.lemma_mod_plus_distr_l",
"FStar.UInt128.carry_sum_ok",
"FStar.UInt.uint_t",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.add_mod",
"FStar.UInt128.carry",
"FStar.UInt64.t",
"Prims.l_True"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | val add_mod: a:t -> b:t -> Pure t
(requires True)
(ensures (fun c -> (v a + v b) % pow2 n = v c)) | [] | FStar.UInt128.add_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 282,
"start_col": 53,
"start_line": 242
} |
Prims.Pure | val 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)) | [
{
"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
}
] | false | 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 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 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)) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"FStar.UInt64.add",
"Prims.unit",
"FStar.UInt128.mod_mul_pow2",
"FStar.UInt64.v",
"Prims.op_Subtraction",
"Prims._assert",
"Prims.pow2",
"FStar.UInt128.pow2_div_bound",
"FStar.UInt64.n",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.mod_mul",
"FStar.Math.Lemmas.pow2_plus",
"FStar.Mul.op_Star",
"Prims.op_Modulus",
"Prims.op_Division",
"FStar.UInt.uint_t",
"FStar.UInt64.shift_left",
"FStar.UInt32.sub",
"FStar.UInt128.u32_64",
"FStar.UInt64.shift_right",
"Prims.op_disEquality",
"Prims.op_Addition"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.add_u64_shift_right | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64}
-> Prims.Pure FStar.UInt64.t | {
"end_col": 18,
"end_line": 728,
"start_col": 73,
"start_line": 716
} |
FStar.Pervasives.Lemma | val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2) | [
{
"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
}
] | false | 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 | 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 = | false | null | true | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.Math.Lemmas.pow2_plus",
"Prims.op_Subtraction",
"Prims.unit",
"FStar.UInt128.mul_div_cancel",
"FStar.Mul.op_Star",
"Prims.pow2",
"FStar.Math.Lemmas.paren_mul_right"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} ->
Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 n2) | [] | FStar.UInt128.mul_pow2_diff | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.nat -> n1: Prims.nat -> n2: Prims.nat{n2 <= n1}
-> FStar.Pervasives.Lemma (ensures a * Prims.pow2 (n1 - n2) == a * Prims.pow2 n1 / Prims.pow2 n2) | {
"end_col": 29,
"end_line": 735,
"start_col": 2,
"start_line": 733
} |
FStar.Pervasives.Lemma | 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)] | [
{
"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
}
] | false | let u64_and_0 a b = UInt.logand_lemma_1 (U64.v a) | 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 = | false | null | true | UInt.logand_lemma_1 (U64.v a) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt.logand_lemma_1",
"FStar.UInt64.n",
"FStar.UInt64.v",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)] | [] | FStar.UInt128.u64_and_0 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt64.v b = 0 ==> FStar.UInt64.v (FStar.UInt64.logand a b) = 0)
[SMTPat (FStar.UInt64.logand a b)] | {
"end_col": 49,
"end_line": 823,
"start_col": 20,
"start_line": 823
} |
FStar.Pervasives.Lemma | val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1) | [
{
"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
}
] | false | let div_product_comm n1 k1 k2 =
div_product n1 k1 k2;
div_product n1 k2 k1 | val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1)
let div_product_comm n1 k1 k2 = | false | null | true | div_product n1 k1 k2;
div_product n1 k2 k1 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"FStar.UInt128.div_product",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val div_product_comm : n1:nat -> k1:pos -> k2:pos ->
Lemma (n1 / k1 / k2 == n1 / k2 / k1) | [] | FStar.UInt128.div_product_comm | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n1: Prims.nat -> k1: Prims.pos -> k2: Prims.pos
-> FStar.Pervasives.Lemma (ensures n1 / k1 / k2 == n1 / k2 / k1) | {
"end_col": 24,
"end_line": 754,
"start_col": 4,
"start_line": 753
} |
Prims.Pure | val 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)) | [
{
"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
}
] | false | 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 | val 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 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)) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.unit",
"FStar.UInt128.mul_pow2_diff",
"FStar.UInt64.v",
"FStar.UInt.uint_t",
"FStar.UInt128.add_u64_shift_right",
"Prims.op_disEquality",
"Prims.int",
"Prims.eq2",
"Prims.op_Addition",
"Prims.op_Division",
"Prims.pow2",
"Prims.op_Modulus",
"FStar.Mul.op_Star"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)) | [] | FStar.UInt128.add_u64_shift_right_respec | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64}
-> Prims.Pure FStar.UInt64.t | {
"end_col": 3,
"end_line": 744,
"start_col": 78,
"start_line": 740
} |
Prims.Pure | val shift_right: a:t -> s:UInt32.t -> Pure t
(requires (U32.v s < n))
(ensures (fun c -> v c = (v a / (pow2 (UInt32.v s))))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 | val shift_right: a:t -> s:UInt32.t -> Pure t
(requires (U32.v s < n))
(ensures (fun c -> v c = (v a / (pow2 (UInt32.v s)))))
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))) = | false | null | false | if U32.lt s u32_64 then shift_right_small a s else shift_right_large a s | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt32.t",
"FStar.UInt32.lt",
"FStar.UInt128.u32_64",
"FStar.UInt128.shift_right_small",
"Prims.bool",
"FStar.UInt128.shift_right_large",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.v",
"Prims.op_Division",
"Prims.pow2"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_right: a:t -> s:UInt32.t -> Pure t
(requires (U32.v s < n))
(ensures (fun c -> v c = (v a / (pow2 (UInt32.v s))))) | [] | FStar.UInt128.shift_right | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 30,
"end_line": 805,
"start_col": 2,
"start_line": 803
} |
Prims.Pure | val lte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lte #n (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let lte (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lte a.low b.low) | val lte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lte #n (v a) (v b)))
let lte (a b: t) = | false | null | false | U64.lt a.high b.high || (U64.eq a.high b.high && U64.lte a.low b.low) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.op_BarBar",
"FStar.UInt64.lt",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_AmpAmp",
"FStar.UInt64.eq",
"FStar.UInt64.lte",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims.bool"
] | [] | (*
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 || | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lte #n (v a) (v b))) | [] | FStar.UInt128.lte | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool | {
"end_col": 63,
"end_line": 815,
"start_col": 18,
"start_line": 814
} |
FStar.Pervasives.Lemma | val 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)] | [
{
"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
}
] | false | 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_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)]
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)] = | false | null | true | u64_logor_comm a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt128.u64_logor_comm",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt64.v",
"Prims.op_Subtraction",
"Prims.pow2",
"FStar.UInt64.logor",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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)] | [] | FStar.UInt128.u64_1_or | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v a = Prims.pow2 64 - 1 ==>
FStar.UInt64.v (FStar.UInt64.logor a b) = Prims.pow2 64 - 1)
[SMTPat (FStar.UInt64.logor a b)] | {
"end_col": 20,
"end_line": 864,
"start_col": 2,
"start_line": 864
} |
Prims.Pure | val lt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lt #n (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let lt (a b:t) = U64.lt a.high b.high ||
(U64.eq a.high b.high && U64.lt a.low b.low) | val lt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lt #n (v a) (v b)))
let lt (a b: t) = | false | null | false | U64.lt a.high b.high || (U64.eq a.high b.high && U64.lt a.low b.low) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.op_BarBar",
"FStar.UInt64.lt",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_AmpAmp",
"FStar.UInt64.eq",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims.bool"
] | [] | (*
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 || | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val lt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == lt #n (v a) (v b))) | [] | FStar.UInt128.lt | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool | {
"end_col": 61,
"end_line": 811,
"start_col": 17,
"start_line": 810
} |
Prims.Pure | val gte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gte #n (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let gte (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gte a.low b.low) | val gte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gte #n (v a) (v b)))
let gte (a b: t) = | false | null | false | U64.gt a.high b.high || (U64.eq a.high b.high && U64.gte a.low b.low) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.op_BarBar",
"FStar.UInt64.gt",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_AmpAmp",
"FStar.UInt64.eq",
"FStar.UInt64.gte",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims.bool"
] | [] | (*
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 || | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gte (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gte #n (v a) (v b))) | [] | FStar.UInt128.gte | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool | {
"end_col": 63,
"end_line": 813,
"start_col": 18,
"start_line": 812
} |
FStar.Pervasives.Lemma | val u64_0_and (a b: U64.t)
: Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0) [SMTPat (U64.logand a b)] | [
{
"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
}
] | false | 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_0_and (a b: U64.t)
: Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0) [SMTPat (U64.logand a b)]
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)] = | false | null | true | u64_logand_comm a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt128.u64_logand_comm",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Equality",
"Prims.int",
"FStar.UInt64.v",
"FStar.UInt64.logand",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_0_and (a b: U64.t)
: Lemma (U64.v a = 0 ==> U64.v (U64.logand a b) = 0) [SMTPat (U64.logand a b)] | [] | FStar.UInt128.u64_0_and | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt64.v a = 0 ==> FStar.UInt64.v (FStar.UInt64.logand a b) = 0)
[SMTPat (FStar.UInt64.logand a b)] | {
"end_col": 21,
"end_line": 828,
"start_col": 2,
"start_line": 828
} |
FStar.Pervasives.Lemma | val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)] | [
{
"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
}
] | false | let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 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 = | false | null | true | UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt.nth_lemma",
"FStar.UInt.lognot",
"FStar.UInt.ones",
"FStar.UInt.zero",
"Prims.unit"
] | [] | (*
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)] | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_not_1 (a:U64.t) :
Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0)
[SMTPat (U64.lognot a)] | [] | FStar.UInt128.u64_not_1 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt64.v a = Prims.pow2 64 - 1 ==> FStar.UInt64.v (FStar.UInt64.lognot a) = 0)
[SMTPat (FStar.UInt64.lognot a)] | {
"end_col": 64,
"end_line": 880,
"start_col": 2,
"start_line": 880
} |
FStar.Pervasives.Lemma | 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)] | [
{
"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
}
] | false | let u64_or_1 a b = UInt.logor_lemma_2 (U64.v a) | 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 = | false | null | true | UInt.logor_lemma_2 (U64.v a) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt.logor_lemma_2",
"FStar.UInt64.n",
"FStar.UInt64.v",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)] | [] | FStar.UInt128.u64_or_1 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> b: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures
FStar.UInt64.v b = Prims.pow2 64 - 1 ==>
FStar.UInt64.v (FStar.UInt64.logor a b) = Prims.pow2 64 - 1)
[SMTPat (FStar.UInt64.logor a b)] | {
"end_col": 47,
"end_line": 859,
"start_col": 19,
"start_line": 859
} |
Prims.Pure | val gt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gt #n (v a) (v b))) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | let gt (a b:t) = U64.gt a.high b.high ||
(U64.eq a.high b.high && U64.gt a.low b.low) | val gt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gt #n (v a) (v b)))
let gt (a b: t) = | false | null | false | U64.gt a.high b.high || (U64.eq a.high b.high && U64.gt a.low b.low) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"Prims.op_BarBar",
"FStar.UInt64.gt",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.op_AmpAmp",
"FStar.UInt64.eq",
"FStar.UInt128.__proj__Mkuint128__item__low",
"Prims.bool"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val gt (a:t) (b:t) : Pure bool
(requires True)
(ensures (fun r -> r == gt #n (v a) (v b))) | [] | FStar.UInt128.gt | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool | {
"end_col": 61,
"end_line": 809,
"start_col": 17,
"start_line": 808
} |
Prims.Pure | val 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))) | [
{
"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
}
] | false | 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 | val 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)))
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))) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt128.t",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.UInt32.v",
"FStar.UInt32.eq",
"FStar.UInt32.__uint_to_t",
"Prims.bool",
"Prims.unit",
"FStar.UInt128.u128_div_pow2",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.UInt128.v",
"Prims.op_Addition",
"FStar.Mul.op_Star",
"Prims.pow2",
"Prims.op_Subtraction",
"Prims.op_Division",
"FStar.UInt128.shift_right_reconstruct",
"FStar.UInt.uint_t",
"FStar.UInt64.v",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt128.add_u64_shift_right_respec",
"FStar.UInt64.shift_right",
"Prims.l_True"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val 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))) | [] | FStar.UInt128.shift_right_small | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt128.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 786,
"start_col": 2,
"start_line": 776
} |
FStar.Pervasives.Lemma | val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)] | [
{
"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
}
] | false | let u64_not_0 a = UInt.lognot_lemma_1 #64 | 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 = | false | null | true | UInt.lognot_lemma_1 #64 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"FStar.UInt64.t",
"FStar.UInt.lognot_lemma_1",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_not_0 (a:U64.t) :
Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1)
[SMTPat (U64.lognot a)] | [] | FStar.UInt128.u64_not_0 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t
-> FStar.Pervasives.Lemma
(ensures FStar.UInt64.v a = 0 ==> FStar.UInt64.v (FStar.UInt64.lognot a) = Prims.pow2 64 - 1)
[SMTPat (FStar.UInt64.lognot a)] | {
"end_col": 41,
"end_line": 874,
"start_col": 18,
"start_line": 874
} |
Prims.Tot | val u64_l32_mask:x: U64.t{U64.v x == pow2 32 - 1} | [
{
"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
}
] | false | let u64_l32_mask: x:U64.t{U64.v x == pow2 32 - 1} = U64.uint_to_t 0xffffffff | 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} = | false | null | false | U64.uint_to_t 0xffffffff | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.uint_to_t"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_l32_mask:x: U64.t{U64.v x == pow2 32 - 1} | [] | FStar.UInt128.u64_l32_mask | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t{FStar.UInt64.v x == Prims.pow2 32 - 1} | {
"end_col": 76,
"end_line": 899,
"start_col": 52,
"start_line": 899
} |
Prims.Tot | val phl (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [
{
"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
}
] | 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)) | 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} = | false | null | false | mul32_bound (h32 (U64.v x)) (l32 (U64.v y)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val phl (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [] | FStar.UInt128.phl | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | {
"end_col": 45,
"end_line": 981,
"start_col": 2,
"start_line": 981
} |
Prims.Tot | val pll (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [
{
"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
}
] | 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)) | 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} = | false | null | false | mul32_bound (l32 (U64.v x)) (l32 (U64.v y)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val pll (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [] | FStar.UInt128.pll | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | {
"end_col": 45,
"end_line": 977,
"start_col": 2,
"start_line": 977
} |
Prims.Tot | val l32 (x: UInt.uint_t 64) : UInt.uint_t 32 | [
{
"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
}
] | false | let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32 | val l32 (x: UInt.uint_t 64) : UInt.uint_t 32
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = | false | null | false | x % pow2 32 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt.uint_t",
"Prims.op_Modulus",
"Prims.pow2"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val l32 (x: UInt.uint_t 64) : UInt.uint_t 32 | [] | FStar.UInt128.l32 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt.uint_t 64 -> FStar.UInt.uint_t 32 | {
"end_col": 58,
"end_line": 967,
"start_col": 47,
"start_line": 967
} |
Prims.Tot | val phh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [
{
"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
}
] | false | 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)) | val phh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1}
let phh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} = | false | null | false | mul32_bound (h32 (U64.v x)) (h32 (U64.v y)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt64.t",
"FStar.UInt128.mul32_bound",
"FStar.UInt128.h32",
"FStar.UInt64.v",
"FStar.UInt.uint_t",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"Prims.pow2"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val phh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1} | [] | FStar.UInt128.phh | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1} | {
"end_col": 45,
"end_line": 983,
"start_col": 2,
"start_line": 983
} |
Prims.Tot | 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} | [
{
"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
}
] | false | let mul32_bound x y =
u32_product_bound x y;
x * y | 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 = | false | null | false | u32_product_bound x y;
x * y | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt.uint_t",
"FStar.Mul.op_Star",
"Prims.unit",
"FStar.UInt128.u32_product_bound",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.eq2",
"Prims.int"
] | [] | (*
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} | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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} | [] | FStar.UInt128.mul32_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt.uint_t 32 -> y: FStar.UInt.uint_t 32
-> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1 /\ n == x * y} | {
"end_col": 7,
"end_line": 974,
"start_col": 2,
"start_line": 973
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | false | let u32_product_bound a b =
uint_product_bound #32 a b | 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 = | false | null | true | uint_product_bound #32 a b | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.pow2",
"FStar.UInt128.uint_product_bound",
"Prims.unit"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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) | [] | FStar.UInt128.u32_product_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 28,
"end_line": 938,
"start_col": 2,
"start_line": 938
} |
Prims.Tot | [
{
"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
}
] | 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 | let mul_wide_low (x y: U64.t) = | false | null | false | (plh x y + (phl x y + pll_h x y) % pow2 32) * pow2 32 % pow2 64 + pll_l x y | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) | false | true | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_wide_low : x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | [] | FStar.UInt128.mul_wide_low | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t -> Prims.int | {
"end_col": 107,
"end_line": 990,
"start_col": 32,
"start_line": 990
} |
|
Prims.Pure | val u64_mod_32 (a: U64.t)
: Pure U64.t (requires True) (ensures (fun r -> U64.v r = U64.v a % pow2 32)) | [
{
"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
}
] | 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 | 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)) = | false | null | false | UInt.logand_mask (U64.v a) 32;
U64.logand a u64_l32_mask | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"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"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_mod_32 (a: U64.t)
: Pure U64.t (requires True) (ensures (fun r -> U64.v r = U64.v a % pow2 32)) | [] | FStar.UInt128.u64_mod_32 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 27,
"end_line": 905,
"start_col": 2,
"start_line": 904
} |
FStar.Pervasives.Lemma | val u64_32_digits (a: U64.t) : Lemma ((U64.v a / pow2 32) * pow2 32 + U64.v a % pow2 32 == U64.v a) | [
{
"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
}
] | 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) | 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) = | false | null | true | div_mod (U64.v a) (pow2 32) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val u64_32_digits (a: U64.t) : Lemma ((U64.v a / pow2 32) * pow2 32 + U64.v a % pow2 32 == U64.v a) | [] | FStar.UInt128.u64_32_digits | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 29,
"end_line": 908,
"start_col": 2,
"start_line": 908
} |
Prims.Pure | val mul32: x:U64.t -> y:U32.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U32.v y)) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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 | val mul32: x:U64.t -> y:U32.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U32.v y))
let mul32 x y = | false | null | false | 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);
let x1y' = U64.mul x1 (FStar.Int.Cast.uint32_to_uint64 y) in
let x1y = U64.add x1y' x0yh in
let r = { low = u32_combine x1y x0yl; high = U64.shift_right x1y u32_32 } in
u64_32_digits x;
assert (U64.v x0y == U64.v x0 * U32.v y);
u64_32_digits x0y;
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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"FStar.UInt64.t",
"FStar.UInt32.t",
"Prims.unit",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"FStar.Mul.op_Star",
"FStar.UInt64.v",
"FStar.UInt32.v",
"Prims.op_Addition",
"Prims.pow2",
"FStar.UInt128.mul32_digits",
"Prims.op_Division",
"FStar.UInt128.u64_32_digits",
"FStar.UInt128.uint128",
"FStar.UInt128.Mkuint128",
"FStar.UInt128.u32_combine",
"FStar.UInt64.shift_right",
"FStar.UInt128.u32_32",
"FStar.UInt64.add",
"FStar.UInt64.mul",
"FStar.Int.Cast.uint32_to_uint64",
"FStar.UInt128.u32_product_bound",
"FStar.UInt128.u64_mod_32",
"FStar.UInt128.t"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul32: x:U64.t -> y:U32.t -> Pure t
(requires True)
(ensures (fun r -> v r == U64.v x * U32.v y)) | [] | FStar.UInt128.mul32 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t | {
"end_col": 3,
"end_line": 965,
"start_col": 15,
"start_line": 940
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | false | let uint_product_bound #n a b =
product_bound a b (pow2 n);
Math.pow2_plus n n | 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 = | false | null | true | product_bound a b (pow2 n);
Math.pow2_plus n n | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"FStar.UInt.uint_t",
"FStar.Math.Lemmas.pow2_plus",
"Prims.unit",
"FStar.UInt128.product_bound",
"Prims.pow2"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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) | [] | FStar.UInt128.uint_product_bound | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 20,
"end_line": 933,
"start_col": 2,
"start_line": 932
} |
Prims.Tot | val eq_mask: a:t -> b:t -> Tot (c:t{(v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0)}) | [
{
"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.BitVector",
"short_module": "BV"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "Seq"
},
{
"abbrev": true,
"full_module": "FStar.UInt",
"short_module": "UInt"
},
{
"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
}
] | false | 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; } | val eq_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 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))) = | false | null | false | let mask = U64.logand (U64.eq_mask a.low b.low) (U64.eq_mask a.high b.high) in
{ low = mask; high = mask } | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"total"
] | [
"FStar.UInt128.t",
"FStar.UInt128.Mkuint128",
"FStar.UInt64.t",
"FStar.UInt64.logand",
"FStar.UInt64.eq_mask",
"FStar.UInt128.__proj__Mkuint128__item__low",
"FStar.UInt128.__proj__Mkuint128__item__high",
"Prims.l_True",
"Prims.l_and",
"Prims.l_imp",
"Prims.b2t",
"Prims.op_Equality",
"FStar.UInt.uint_t",
"FStar.UInt128.n",
"FStar.UInt128.v",
"Prims.int",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_disEquality"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val eq_mask: a:t -> b:t -> Tot (c:t{(v a = v b ==> v c = pow2 n - 1) /\ (v a <> v b ==> v c = 0)}) | [] | FStar.UInt128.eq_mask | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) } | {
"end_col": 30,
"end_line": 841,
"start_col": 87,
"start_line": 838
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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)) | 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) = | false | null | true | 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": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 + | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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) | [] | FStar.UInt128.product_expand | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 80,
"end_line": 1077,
"start_col": 2,
"start_line": 1075
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | false | 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) | 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 = | false | null | true | 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) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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" | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 25,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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) | [] | FStar.UInt128.u64_32_product | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} |
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) | {
"end_col": 65,
"end_line": 1068,
"start_col": 2,
"start_line": 1061
} |
Prims.Pure | 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)) | [
{
"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
}
] | 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') | 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)) = | false | null | false | 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": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"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"
] | [] | (*
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 /\ | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.mul_wide_impl_t' | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | x: FStar.UInt64.t -> y: FStar.UInt64.t
-> Prims.Pure (((FStar.UInt64.t * FStar.UInt64.t) * FStar.UInt64.t) * FStar.UInt64.t) | {
"end_col": 18,
"end_line": 1018,
"start_col": 39,
"start_line": 1004
} |
Prims.Tot | 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}) | [
{
"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
}
] | false | 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 | 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 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}) = | false | null | false | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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'"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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}) | [] | FStar.UInt128.mul_wide_impl | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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 } | {
"end_col": 3,
"end_line": 1051,
"start_col": 60,
"start_line": 1029
} |
FStar.Pervasives.Lemma | val add_mod_then_mod (n m: nat) (k: pos) : Lemma ((n + m % k) % k == (n + m) % k) | [
{
"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
}
] | 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 | 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) = | false | null | true | mod_add n m k;
mod_add n (m % k) k;
mod_double m k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val add_mod_then_mod (n m: nat) (k: pos) : Lemma ((n + m % k) % k == (n + m) % k) | [] | FStar.UInt128.add_mod_then_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures (n + m % k) % k == (n + m) % k) | {
"end_col": 16,
"end_line": 1089,
"start_col": 2,
"start_line": 1087
} |
Prims.Pure | 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)) | [
{
"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
}
] | 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) | 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)) = | false | null | false | U64.add lo (U64.shift_left hi u32_32) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [] | [
"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"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 30,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | 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)) | [] | FStar.UInt128.u32_combine | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t | {
"end_col": 39,
"end_line": 920,
"start_col": 2,
"start_line": 920
} |
FStar.Pervasives.Lemma | val shift_add (n: nat) (m: nat{m < pow2 32})
: Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) | [
{
"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
}
] | 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) | 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) = | false | null | true | add_mod_small' m (n * pow2 32) (pow2 64) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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}) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val shift_add (n: nat) (m: nat{m < pow2 32})
: Lemma (n * pow2 32 % pow2 64 + m == (n * pow2 32 + m) % pow2 64) | [] | FStar.UInt128.shift_add | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 40,
"end_line": 1093,
"start_col": 2,
"start_line": 1093
} |
FStar.Pervasives.Lemma | val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0) | [
{
"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
}
] | false | let mod_spec_mod n k =
assert (n - n%k == n / k * k);
Math.multiple_modulo_lemma (n/k) k | val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0)
let mod_spec_mod n k = | false | null | true | assert (n - n % k == (n / k) * k);
Math.multiple_modulo_lemma (n / k) k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mod_spec_mod (n:nat) (k:pos) : Lemma ((n - n%k) % k == 0) | [] | FStar.UInt128.mod_spec_mod | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (ensures (n - n % k) % k == 0) | {
"end_col": 36,
"end_line": 1140,
"start_col": 2,
"start_line": 1139
} |
FStar.Pervasives.Lemma | val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k)) | [
{
"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
}
] | 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;
() | 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 = | false | null | true | Math.distributivity_add_left (n / k) (m / k) k;
div_mod n k;
div_mod m k;
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"Prims.unit",
"FStar.UInt128.div_mod",
"FStar.Math.Lemmas.distributivity_add_left",
"Prims.op_Division"
] | [] | (*
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)) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val div_sum_combine1 : n:nat -> m:nat -> k:pos ->
Lemma ((n / k + m / k) * k == (n - n % k) + (m - m % k)) | [] | FStar.UInt128.div_sum_combine1 | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures (n / k + m / k) * k == n - n % k + (m - m % k)) | {
"end_col": 4,
"end_line": 1152,
"start_col": 2,
"start_line": 1149
} |
FStar.Pervasives.Lemma | 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) | [
{
"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
}
] | 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);
() | 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 = | false | null | true | 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);
() | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | 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) | [] | FStar.UInt128.product_high_expand | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 4,
"end_line": 1129,
"start_col": 2,
"start_line": 1125
} |
FStar.Pervasives.Lemma | val n_minus_mod_exact (n: nat) (k: pos) : Lemma ((n - n % k) % k == 0) | [
{
"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
}
] | false | let n_minus_mod_exact (n:nat) (k:pos) :
Lemma ((n - n % k) % k == 0) =
mod_spec_mod n k;
mod_0 k | 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) = | false | null | true | mod_spec_mod n k;
mod_0 k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val n_minus_mod_exact (n: nat) (k: pos) : Lemma ((n - n % k) % k == 0) | [] | FStar.UInt128.n_minus_mod_exact | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (ensures (n - n % k) % k == 0) | {
"end_col": 11,
"end_line": 1160,
"start_col": 4,
"start_line": 1159
} |
FStar.Pervasives.Lemma | 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)) | [
{
"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
}
] | 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 | 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 = | false | null | true | 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 | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | 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)) | [] | FStar.UInt128.sum_rounded_mod_exact | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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)) | {
"end_col": 46,
"end_line": 1174,
"start_col": 2,
"start_line": 1169
} |
FStar.Pervasives.Lemma | val mul_wide_high_ok (x y: U64.t) : Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) | [
{
"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
}
] | 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) | 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) = | false | null | true | product_high_expand x y;
sum_shift_carry (phl x y + pll_h x y) (plh x y) (pow2 32) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) : | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val mul_wide_high_ok (x y: U64.t) : Lemma ((U64.v x * U64.v y) / pow2 64 == mul_wide_high x y) | [] | FStar.UInt128.mul_wide_high_ok | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | 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) | {
"end_col": 59,
"end_line": 1199,
"start_col": 2,
"start_line": 1198
} |
FStar.Pervasives.Lemma | val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k) | [
{
"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
}
] | false | 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 | 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 = | false | null | true | div_sum_combine a (b + a % k) k;
add_mod_then_mod b a k;
Math.lemma_mod_spec (a + b) k | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"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"
] | [] | (*
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) | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 20,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | null | val sum_shift_carry : a:nat -> b:nat -> k:pos ->
Lemma (a / k + (b + a%k) / k == (a + b) / k) | [] | FStar.UInt128.sum_shift_carry | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | a: Prims.nat -> b: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures a / k + (b + a % k) / k == (a + b) / k) | {
"end_col": 29,
"end_line": 1194,
"start_col": 2,
"start_line": 1190
} |
FStar.Pervasives.Lemma | val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k) | [
{
"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
}
] | false | 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)) | val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k)
let div_sum_combine n m k = | false | null | true | 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)) | {
"checked_file": "FStar.UInt128.fst.checked",
"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"
} | [
"lemma"
] | [
"Prims.nat",
"Prims.pos",
"Prims._assert",
"Prims.eq2",
"Prims.int",
"Prims.op_Subtraction",
"Prims.op_Addition",
"Prims.op_Modulus",
"Prims.unit",
"FStar.UInt128.mul_injective",
"Prims.op_Division",
"FStar.UInt128.div_sum_combine1",
"FStar.UInt128.sum_rounded_mod_exact"
] | [] | (*
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 | false | false | FStar.UInt128.fst | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 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"
} | null | val div_sum_combine : n:nat -> m:nat -> k:pos ->
Lemma (n / k + m / k == (n + (m - n % k) - m % k) / k) | [] | FStar.UInt128.div_sum_combine | {
"file_name": "ulib/FStar.UInt128.fst",
"git_rev": "f4cbb7a38d67eeb13fbdb2f4fb8a44a65cbcdc1f",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | n: Prims.nat -> m: Prims.nat -> k: Prims.pos
-> FStar.Pervasives.Lemma (ensures n / k + m / k == (n + (m - n % k) - m % k) / k) | {
"end_col": 57,
"end_line": 1184,
"start_col": 2,
"start_line": 1181
} |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.