file_name
stringlengths
5
52
name
stringlengths
4
95
original_source_type
stringlengths
0
23k
source_type
stringlengths
9
23k
source_definition
stringlengths
9
57.9k
source
dict
source_range
dict
file_context
stringlengths
0
721k
dependencies
dict
opens_and_abbrevs
listlengths
2
94
vconfig
dict
interleaved
bool
1 class
verbose_type
stringlengths
1
7.42k
effect
stringclasses
118 values
effect_flags
sequencelengths
0
2
mutual_with
sequencelengths
0
11
ideal_premises
sequencelengths
0
236
proof_features
sequencelengths
0
1
is_simple_lemma
bool
2 classes
is_div
bool
2 classes
is_proof
bool
2 classes
is_simply_typed
bool
2 classes
is_type
bool
2 classes
partial_definition
stringlengths
5
3.99k
completed_definiton
stringlengths
1
1.63M
isa_cross_project_example
bool
1 class
FStar.UInt128.fst
FStar.UInt128.lem_ult
val lem_ult (a b: uint_t 64) : Lemma (if a < b then fact1 a b else fact0 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) = int2bv_ult a b; lem_ult_1 a b; lem_ult_2 a b
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 96, "start_col": 0, "start_line": 90 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.int2bv_ult
val int2bv_ult (#n: pos) (a b: uint_t n) : Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
val int2bv_ult (#n: pos) (a b: uint_t n) : Lemma (ensures a < b <==> bvult #n (int2bv #n a) (int2bv #n b))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 88, "start_col": 0, "start_line": 83 }
(* 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)'")
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt.uint_t n -> b: FStar.UInt.uint_t n -> FStar.Pervasives.Lemma (ensures a < b <==> FStar.BV.bvult (FStar.BV.int2bv a) (FStar.BV.int2bv b))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.pos", "FStar.UInt.uint_t", "FStar.Classical.Sugar.implies_intro", "Prims.b2t", "FStar.BV.bvult", "FStar.BV.int2bv", "Prims.squash", "Prims.op_LessThan", "FStar.BV.int2bv_lemma_ult_2", "Prims.unit", "FStar.BV.int2bv_lemma_ult_1", "Prims.l_True", "Prims.l_iff", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.t
val t: (x:Type0{hasEq x})
val t: (x:Type0{hasEq x})
let t = uint128
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 113, "start_col": 0, "start_line": 113 }
(* 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 }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: Type0{Prims.hasEq x}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt128.uint128" ]
[]
false
false
false
false
false
let t =
uint128
false
FStar.UInt128.fst
FStar.UInt128.v
val v (x:t) : Tot (uint_t n)
val v (x:t) : Tot (uint_t n)
let v x = U64.v x.low + (U64.v x.high) * (pow2 64)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 119, "start_col": 0, "start_line": 119 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt128.t -> FStar.UInt.uint_t FStar.UInt128.n
Prims.Tot
[ "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" ]
[]
false
false
false
true
false
let v x =
U64.v x.low + (U64.v x.high) * (pow2 64)
false
FStar.UInt128.fst
FStar.UInt128.mod_mul
val mod_mul: n:nat -> k1:pos -> k2:pos -> Lemma ((n % k2) * k1 == (n * k1) % (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 = Math.modulo_scale_lemma n k1 k2
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 221, "start_col": 0, "start_line": 220 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> k1: Prims.pos -> k2: Prims.pos -> FStar.Pervasives.Lemma (ensures (n % k2) * k1 == n * k1 % (k1 * k2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.pos", "FStar.Math.Lemmas.modulo_scale_lemma", "Prims.unit" ]
[]
true
false
true
false
false
let mod_mul n k1 k2 =
Math.modulo_scale_lemma n k1 k2
false
FStar.UInt128.fst
FStar.UInt128.mod_mod
val mod_mod: a:nat -> k:nat{k>0} -> k':nat{k'>0} -> Lemma ((a % k) % (k'*k) == a % 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' = assert (a % k < k); assert (a % k < k' * k)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 203, "start_col": 0, "start_line": 201 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat -> k: Prims.nat{k > 0} -> k': Prims.nat{k' > 0} -> FStar.Pervasives.Lemma (ensures a % k % (k' * k) == a % k)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "Prims._assert", "Prims.op_LessThan", "Prims.op_Modulus", "FStar.Mul.op_Star", "Prims.unit" ]
[]
true
false
true
false
false
let mod_mod a k k' =
assert (a % k < k); assert (a % k < k' * k)
false
FStar.UInt128.fst
FStar.UInt128.v_inj
val v_inj (x1 x2: t): Lemma (requires (v x1 == v x2)) (ensures (x1 == x2))
val v_inj (x1 x2: t): Lemma (requires (v x1 == v x2)) (ensures (x1 == x2))
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); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 133, "start_col": 0, "start_line": 128 }
(* 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)); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x1: FStar.UInt128.t -> x2: FStar.UInt128.t -> FStar.Pervasives.Lemma (requires FStar.UInt128.v x1 == FStar.UInt128.v x2) (ensures x1 == x2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt128.t", "Prims.unit", "Prims._assert", "Prims.eq2", "FStar.UInt128.uint_to_t", "FStar.UInt128.v", "FStar.UInt.uint_t", "FStar.UInt128.n", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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); ()
false
FStar.UInt128.fst
FStar.UInt128.uint_to_t
val uint_to_t: x:uint_t n -> Pure t (requires True) (ensures (fun y -> v y = x))
val uint_to_t: x:uint_t n -> Pure t (requires True) (ensures (fun y -> v y = 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)); }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 126, "start_col": 0, "start_line": 123 }
(* 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) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt.uint_t FStar.UInt128.n -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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)) }
false
FStar.UInt128.fst
FStar.UInt128.carry
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)))
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))) = constant_time_carry_ok a b; constant_time_carry a b
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 176, "start_col": 0, "start_line": 172 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt64.t -> Prims.Pure FStar.UInt64.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.vec128
val vec128 (a: t) : BV.bv_t 128
val vec128 (a: t) : BV.bv_t 128
let vec128 (a: t) : BV.bv_t 128 = UInt.to_vec #128 (v a)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 388, "start_col": 0, "start_line": 388 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> FStar.BitVector.bv_t 128
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt128.t", "FStar.UInt.to_vec", "FStar.UInt128.v", "FStar.BitVector.bv_t" ]
[]
false
false
false
false
false
let vec128 (a: t) : BV.bv_t 128 =
UInt.to_vec #128 (v a)
false
Hacl.Bignum4096_32.fsti
Hacl.Bignum4096_32.n_bytes
val n_bytes : FStar.UInt32.t
let n_bytes = n_limbs `FStar.UInt32.mul` 4ul
{ "file_name": "code/bignum/Hacl.Bignum4096_32.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 44, "end_line": 18, "start_col": 0, "start_line": 18 }
module Hacl.Bignum4096_32 open FStar.Mul module BN = Hacl.Bignum module BS = Hacl.Bignum.SafeAPI module MA = Hacl.Bignum.MontArithmetic #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" inline_for_extraction noextract let t_limbs: Hacl.Bignum.Definitions.limb_t = Lib.IntTypes.U32 inline_for_extraction noextract let n_limbs: BN.meta_len t_limbs = 128ul
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.IntTypes.fsti.checked", "Hacl.Bignum.SafeAPI.fst.checked", "Hacl.Bignum.MontArithmetic.fsti.checked", "Hacl.Bignum.Definitions.fst.checked", "Hacl.Bignum.Convert.fst.checked", "Hacl.Bignum.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Hacl.Bignum4096_32.fsti" }
[ { "abbrev": true, "full_module": "Hacl.Bignum.MontArithmetic", "short_module": "MA" }, { "abbrev": true, "full_module": "Hacl.Bignum.SafeAPI", "short_module": "BS" }, { "abbrev": true, "full_module": "Hacl.Bignum", "short_module": "BN" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl", "short_module": null }, { "abbrev": false, "full_module": "Hacl", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
FStar.UInt32.t
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt32.mul", "Hacl.Bignum4096_32.n_limbs", "FStar.UInt32.__uint_to_t" ]
[]
false
false
false
true
false
let n_bytes =
n_limbs `FStar.UInt32.mul` 4ul
false
FStar.UInt128.fst
FStar.UInt128.vec64
val vec64 (a: U64.t) : BV.bv_t 64
val vec64 (a: U64.t) : BV.bv_t 64
let vec64 (a: U64.t) : BV.bv_t 64 = UInt.to_vec (U64.v a)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 389, "start_col": 0, "start_line": 389 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> FStar.BitVector.bv_t 64
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.to_vec", "FStar.UInt64.n", "FStar.UInt64.v", "FStar.BitVector.bv_t" ]
[]
false
false
false
false
false
let vec64 (a: U64.t) : BV.bv_t 64 =
UInt.to_vec (U64.v a)
false
FStar.UInt128.fst
FStar.UInt128.div_product
val div_product : n:nat -> m1:nat{m1>0} -> m2:nat{m2>0} -> Lemma (n / (m1*m2) == (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 = Math.division_multiplication_lemma n m1 m2
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 211, "start_col": 0, "start_line": 210 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> m1: Prims.nat{m1 > 0} -> m2: Prims.nat{m2 > 0} -> FStar.Pervasives.Lemma (ensures n / (m1 * m2) == n / m1 / m2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Math.Lemmas.division_multiplication_lemma", "Prims.unit" ]
[]
true
false
true
false
false
let div_product n m1 m2 =
Math.division_multiplication_lemma n m1 m2
false
FStar.UInt128.fst
FStar.UInt128.add
val add: a:t -> b:t -> Pure t (requires (size (v a + v b) n)) (ensures (fun c -> v a + v b = v c))
val add: a:t -> b:t -> Pure t (requires (size (v a + v b) n)) (ensures (fun c -> v a + v b = v c))
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); }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 187, "start_col": 0, "start_line": 181 }
(* 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)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "FStar.UInt128.t", "FStar.UInt128.Mkuint128", "FStar.UInt64.add", "FStar.UInt128.__proj__Mkuint128__item__high", "FStar.UInt128.carry", "FStar.UInt128.__proj__Mkuint128__item__low", "Prims.unit", "FStar.UInt128.carry_sum_ok", "FStar.UInt64.t", "FStar.UInt64.add_mod", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Addition", "FStar.UInt128.v", "Prims.pow2", "Prims.op_Equality", "Prims.int" ]
[]
false
false
false
false
false
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
FStar.UInt128.fst
FStar.UInt128.mod_add
val mod_add: n1:nat -> n2:nat -> k:nat{k > 0} -> Lemma ((n1 % k + n2 % k) % k == (n1 + n2) % 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 228, "start_col": 0, "start_line": 228 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n1: Prims.nat -> n2: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures (n1 % k + n2 % k) % k == (n1 + n2) % k)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Math.Lemmas.modulo_distributivity", "Prims.unit" ]
[]
true
false
true
false
false
let mod_add n1 n2 k =
Math.modulo_distributivity n1 n2 k
false
Hacl.Bignum4096_32.fsti
Hacl.Bignum4096_32.n_limbs
val n_limbs:BN.meta_len t_limbs
val n_limbs:BN.meta_len t_limbs
let n_limbs: BN.meta_len t_limbs = 128ul
{ "file_name": "code/bignum/Hacl.Bignum4096_32.fsti", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 40, "end_line": 15, "start_col": 0, "start_line": 15 }
module Hacl.Bignum4096_32 open FStar.Mul module BN = Hacl.Bignum module BS = Hacl.Bignum.SafeAPI module MA = Hacl.Bignum.MontArithmetic #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" inline_for_extraction noextract let t_limbs: Hacl.Bignum.Definitions.limb_t = Lib.IntTypes.U32
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "Lib.IntTypes.fsti.checked", "Hacl.Bignum.SafeAPI.fst.checked", "Hacl.Bignum.MontArithmetic.fsti.checked", "Hacl.Bignum.Definitions.fst.checked", "Hacl.Bignum.Convert.fst.checked", "Hacl.Bignum.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Hacl.Bignum4096_32.fsti" }
[ { "abbrev": true, "full_module": "Hacl.Bignum.MontArithmetic", "short_module": "MA" }, { "abbrev": true, "full_module": "Hacl.Bignum.SafeAPI", "short_module": "BS" }, { "abbrev": true, "full_module": "Hacl.Bignum", "short_module": "BN" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Hacl", "short_module": null }, { "abbrev": false, "full_module": "Hacl", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Hacl.Bignum.meta_len Hacl.Bignum4096_32.t_limbs
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt32.__uint_to_t" ]
[]
false
false
false
true
false
let n_limbs:BN.meta_len t_limbs =
128ul
false
FStar.UInt128.fst
FStar.UInt128.mod_spec_rew_n
val mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + 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) = mod_spec n k
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 224, "start_col": 0, "start_line": 223 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n == (n / k) * k + n % k)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
let mod_spec_rew_n (n: nat) (k: nat{k > 0}) : Lemma (n == (n / k) * k + n % k) =
mod_spec n k
false
FStar.UInt128.fst
FStar.UInt128.mod_add_small
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))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 235, "start_col": 0, "start_line": 233 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n1: Prims.nat -> n2: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (requires n1 % k + n2 % k < k) (ensures n1 % k + n2 % k == (n1 + n2) % k)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Math.Lemmas.small_modulo_lemma_1", "Prims.op_Addition", "Prims.op_Modulus", "Prims.unit", "FStar.UInt128.mod_add" ]
[]
true
false
true
false
false
let mod_add_small n1 n2 k =
mod_add n1 n2 k; Math.small_modulo_lemma_1 (n1 % k + n2 % k) k
false
FStar.UInt128.fst
FStar.UInt128.sub_mod_wrap1_ok
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))
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 = // 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 334, "start_col": 0, "start_line": 323 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 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" }
false
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)
FStar.Pervasives.Lemma
[ "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" ]
[]
false
false
true
false
false
let sub_mod_wrap1_ok a b =
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; ())
false
FStar.UInt128.fst
FStar.UInt128.add_underspec
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))
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) = 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); }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 83, "end_line": 197, "start_col": 0, "start_line": 189 }
(* 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); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
false
let add_underspec (a b: t) =
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) }
false
FStar.UInt128.fst
FStar.UInt128.sub_mod_wrap2_ok
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))
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)) = // 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); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 352, "start_col": 0, "start_line": 341 }
(* 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)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> 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)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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)) =
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); ()
false
FStar.UInt128.fst
FStar.UInt128.sub_mod_pos_ok
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))
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)) = assert (sub a b == sub_mod_impl a b); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 309, "start_col": 0, "start_line": 305 }
(* 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); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 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" }
false
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)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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); ()
false
FStar.UInt128.fst
FStar.UInt128.mul_div_cancel
val mul_div_cancel : n:nat -> k:nat{k>0} -> Lemma ((n * k) / k == n)
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 216, "start_col": 0, "start_line": 215 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n * k / k == n)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.Math.Lemmas.cancel_mul_div", "Prims.unit" ]
[]
true
false
true
false
false
let mul_div_cancel n k =
Math.cancel_mul_div n k
false
FStar.UInt128.fst
FStar.UInt128.sub_mod
val sub_mod: a:t -> b:t -> Pure t (requires True) (ensures (fun c -> (v a - v b) % pow2 n = v c))
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)) = (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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 368, "start_col": 0, "start_line": 362 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 40, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.constant_time_carry_ok
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))
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)) = 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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 98, "end_line": 170, "start_col": 0, "start_line": 144 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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 -> 1uL | _ -> 0uL))
FStar.Pervasives.Lemma
[ "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.Stubs.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" ]
[]
false
false
true
false
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); }; 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)
false
FStar.UInt128.fst
FStar.UInt128.sub
val sub: a:t -> b:t -> Pure t (requires (size (v a - v b) n)) (ensures (fun c -> v a - v b = v c))
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)) = 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); }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 291, "start_col": 0, "start_line": 286 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 5, "quake_keep": false, "quake_lo": 1, "retry": true, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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) }
false
FStar.UInt128.fst
FStar.UInt128.sub_mod_wrap_ok
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))
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)) = if U64.v a.low < U64.v b.low then sub_mod_wrap1_ok a b else sub_mod_wrap2_ok a b
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 359, "start_col": 0, "start_line": 354 }
(* 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); ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> 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)
FStar.Pervasives.Lemma
[ "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" ]
[]
false
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.sub_underspec
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))
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) = 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); }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 81, "end_line": 297, "start_col": 0, "start_line": 294 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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) }
false
FStar.UInt128.fst
FStar.UInt128.to_vec_append
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))
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 386, "start_col": 0, "start_line": 385 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
num1: FStar.UInt.uint_t n1 -> num2: FStar.UInt.uint_t n2 -> FStar.Pervasives.Lemma (ensures FStar.UInt.to_vec (FStar.UInt128.append_uint num1 num2) == FStar.Seq.Base.append (FStar.UInt.to_vec num2) (FStar.UInt.to_vec num1))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.UInt.uint_t", "FStar.UInt.append_lemma", "FStar.UInt.to_vec", "Prims.unit" ]
[]
true
false
true
false
false
let to_vec_append #n1 #n2 num1 num2 =
UInt.append_lemma (UInt.to_vec num2) (UInt.to_vec num1)
false
FStar.UInt128.fst
FStar.UInt128.append_uint
val append_uint : #n1:nat -> #n2:nat -> num1:UInt.uint_t n1 -> num2:UInt.uint_t n2 -> UInt.uint_t (n1+n2)
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 23, "end_line": 381, "start_col": 0, "start_line": 379 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
num1: FStar.UInt.uint_t n1 -> num2: FStar.UInt.uint_t n2 -> FStar.UInt.uint_t (n1 + n2)
Prims.Tot
[ "total" ]
[]
[ "Prims.nat", "FStar.UInt.uint_t", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.pow2", "Prims.unit", "FStar.UInt128.shift_bound" ]
[]
false
false
false
false
false
let append_uint #n1 #n2 num1 num2 =
shift_bound num2 n1; num1 + num2 * pow2 n1
false
FStar.UInt128.fst
FStar.UInt128.to_vec_v
val to_vec_v (a: t) : Lemma (vec128 a == Seq.append (vec64 a.high) (vec64 a.low))
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)) = to_vec_append (U64.v a.low) (U64.v a.high)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 393, "start_col": 0, "start_line": 391 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> FStar.Pervasives.Lemma (ensures FStar.UInt128.vec128 a == FStar.Seq.Base.append (FStar.UInt128.vec64 (Mkuint128?.high a)) (FStar.UInt128.vec64 (Mkuint128?.low a)))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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)
false
FStar.UInt128.fst
FStar.UInt128.shift_bound
val shift_bound : #n:nat -> num:UInt.uint_t n -> n':nat -> Lemma (num * pow2 n' <= pow2 (n'+n) - pow2 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' = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 376, "start_col": 0, "start_line": 373 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
num: FStar.UInt.uint_t n -> n': Prims.nat -> FStar.Pervasives.Lemma (ensures num * Prims.pow2 n' <= Prims.pow2 (n' + n) - Prims.pow2 n')
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.logxor_vec_append
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))
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))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 421, "start_col": 0, "start_line": 419 }
(* 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) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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))
false
FStar.UInt128.fst
FStar.UInt128.logand
val logand: a:t -> b:t -> Pure t (requires True) (ensures (fun r -> v r == logand (v a) (v b)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 414, "start_col": 0, "start_line": 402 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.logor_vec_append
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))
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))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 84, "end_line": 442, "start_col": 0, "start_line": 440 }
(* 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) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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))
false
FStar.UInt128.fst
FStar.UInt128.shift_past_mod
val shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0)
val shift_past_mod (n k1: nat) (k2: nat{k2 >= k1}) : Lemma ((n * pow2 k2) % pow2 k1 == 0)
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 488, "start_col": 0, "start_line": 483 }
(* 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; ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> k1: Prims.nat -> k2: Prims.nat{k2 >= k1} -> FStar.Pervasives.Lemma (ensures n * Prims.pow2 k2 % Prims.pow2 k1 == 0)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
let shift_past_mod (n 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)
false
FStar.UInt128.fst
FStar.UInt128.logxor
val logxor: a:t -> b:t -> Pure t (requires True) (ensures (fun r -> v r == logxor (v a) (v b)))
val logxor: a:t -> b:t -> Pure t (requires True) (ensures (fun r -> v r == logxor (v a) (v b)))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 435, "start_col": 0, "start_line": 423 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "FStar.UInt128.t", "Prims.unit", "Prims._assert", "Prims.eq2", "FStar.BitVector.bv_t", "FStar.UInt128.vec128", "FStar.BitVector.logxor_vec", "FStar.UInt128.to_vec_v", "FStar.UInt128.logxor_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.logxor", "Prims.l_True", "Prims.b2t", "Prims.op_Equality", "FStar.UInt.uint_t", "FStar.UInt128.n", "FStar.UInt128.v", "FStar.UInt.logxor" ]
[]
false
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.lognot
val lognot: a:t -> Pure t (requires True) (ensures (fun r -> v r == lognot (v a)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 475, "start_col": 0, "start_line": 465 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.mod_mul_cancel
val mod_mul_cancel (n: nat) (k: nat{k > 0}) : Lemma ((n * k) % k == 0)
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) = mod_spec (n * k) k; mul_div_cancel n k; ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 481, "start_col": 0, "start_line": 477 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures n * k % k == 0)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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; ()
false
FStar.UInt128.fst
FStar.UInt128.shift_left_large_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))
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: 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)); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 509, "start_col": 0, "start_line": 502 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 40, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
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)) =
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)); ()
false
FStar.UInt128.fst
FStar.UInt128.lognot_vec_append
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))
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))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 67, "end_line": 463, "start_col": 0, "start_line": 461 }
(* 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) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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))
false
FStar.UInt128.fst
FStar.UInt128.u32_64
val u32_64:n: U32.t{U32.v n == 64}
val u32_64:n: U32.t{U32.v n == 64}
let u32_64: n:U32.t{U32.v n == 64} = U32.uint_to_t 64
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 519, "start_col": 8, "start_line": 519 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: FStar.UInt32.t{FStar.UInt32.v n == 64}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt32.uint_to_t" ]
[]
false
false
false
false
false
let u32_64:n: U32.t{U32.v n == 64} =
U32.uint_to_t 64
false
FStar.UInt128.fst
FStar.UInt128.shift_left_large_val
val shift_left_large_val (#n1 #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)))
val shift_left_large_val (#n1 #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)))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 499, "start_col": 0, "start_line": 495 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a1: FStar.UInt.uint_t n1 -> a2: FStar.UInt.uint_t n2 -> s: Prims.nat -> FStar.Pervasives.Lemma (ensures (a1 + a2 * Prims.pow2 n1) * Prims.pow2 s == a1 * Prims.pow2 s + a2 * Prims.pow2 (n1 + s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "FStar.UInt.uint_t", "FStar.Math.Lemmas.pow2_plus", "Prims.unit", "FStar.Math.Lemmas.paren_mul_right", "Prims.pow2", "FStar.Math.Lemmas.distributivity_add_left", "FStar.Mul.op_Star", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.int", "Prims.op_Addition", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let shift_left_large_val (#n1 #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
false
FStar.UInt128.fst
FStar.UInt128.logand_vec_append
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))
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))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 400, "start_col": 0, "start_line": 398 }
(* 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) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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))
false
FStar.UInt128.fst
FStar.UInt128.shift_left_large_lemma_t
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))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 63, "end_line": 517, "start_col": 0, "start_line": 516 }
(* 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 ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> 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)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
let shift_left_large_lemma_t a s =
shift_left_large_lemma #64 #64 (U64.v a.low) (U64.v a.high) s
false
FStar.UInt128.fst
FStar.UInt128.logor
val logor: a:t -> b:t -> Pure t (requires True) (ensures (fun r -> v r == logor (v a) (v b)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 456, "start_col": 0, "start_line": 444 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.add_mod_small'
val add_mod_small' : n:nat -> m:nat -> k:pos -> Lemma (requires (n + m % k < k)) (ensures (n + m % k == (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 = Math.lemma_mod_lt (n + m % k) k; Math.modulo_lemma n k; mod_add n m k
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 620, "start_col": 0, "start_line": 617 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> m: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (requires n + m % k < k) (ensures n + m % k == (n + m) % k)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_t_mod_val'
val 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)
val 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 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; ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 664, "start_col": 0, "start_line": 653 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: Prims.nat{s < 64} -> FStar.Pervasives.Lemma (ensures FStar.UInt128.v a * Prims.pow2 s % Prims.pow2 128 == FStar.UInt64.v (Mkuint128?.low a) * Prims.pow2 s + FStar.UInt64.v (Mkuint128?.high a) * Prims.pow2 (64 + s) % Prims.pow2 128)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt128.t", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Prims.unit", "FStar.UInt128.shift_t_val", "FStar.UInt128.add_mod_small'", "FStar.Mul.op_Star", "Prims.pow2", "Prims.op_Addition", "FStar.UInt128.add_lt_le", "Prims.op_Modulus", "Prims.op_Subtraction", "FStar.UInt128.mul_mod_bound", "FStar.UInt128.u64_pow2_bound", "FStar.UInt.uint_t", "FStar.UInt64.v", "FStar.UInt128.__proj__Mkuint128__item__high", "FStar.UInt128.__proj__Mkuint128__item__low", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.int", "FStar.UInt128.v", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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; 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; ()
false
FStar.UInt128.fst
FStar.UInt128.div_pow2_diff
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))
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 528, "start_col": 0, "start_line": 524 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat -> n1: Prims.nat -> n2: Prims.nat{n2 <= n1} -> FStar.Pervasives.Lemma (ensures a / Prims.pow2 (n1 - n2) == a * Prims.pow2 n2 / Prims.pow2 n1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt128.mul_div_cancel", "Prims.pow2", "Prims.unit", "FStar.UInt128.div_product", "FStar.Mul.op_Star", "Prims.op_Subtraction", "Prims._assert", "Prims.eq2", "Prims.int", "Prims.op_Division", "FStar.Math.Lemmas.pow2_plus" ]
[]
true
false
true
false
false
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)
false
FStar.UInt128.fst
FStar.UInt128.mod_double
val mod_double: a:nat -> k:nat{k>0} -> Lemma (a % k % k == a % k)
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 493, "start_col": 0, "start_line": 492 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat -> k: Prims.nat{k > 0} -> FStar.Pervasives.Lemma (ensures a % k % k == a % k)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.b2t", "Prims.op_GreaterThan", "FStar.UInt128.mod_mod", "Prims.unit" ]
[]
true
false
true
false
false
let mod_double a k =
mod_mod a k 1
false
FStar.UInt128.fst
FStar.UInt128.mul_pow2_diff
val mul_pow2_diff: a:nat -> n1:nat -> n2:nat{n2 <= n1} -> Lemma (a * pow2 (n1 - n2) == a * pow2 n1 / pow2 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 = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 735, "start_col": 0, "start_line": 732 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat -> n1: Prims.nat -> n2: Prims.nat{n2 <= n1} -> FStar.Pervasives.Lemma (ensures a * Prims.pow2 (n1 - n2) == a * Prims.pow2 n1 / Prims.pow2 n2)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.mod_mul_pow2
val mod_mul_pow2 : n:nat -> e1:nat -> e2:nat -> Lemma (n % pow2 e1 * pow2 e2 <= pow2 (e1+e2) - pow2 e2)
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 536, "start_col": 0, "start_line": 532 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> e1: Prims.nat -> e2: Prims.nat -> FStar.Pervasives.Lemma (ensures (n % Prims.pow2 e1) * Prims.pow2 e2 <= Prims.pow2 (e1 + e2) - Prims.pow2 e2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "FStar.Math.Lemmas.pow2_plus", "Prims.unit", "Prims._assert", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Mul.op_Star", "Prims.op_Modulus", "Prims.pow2", "Prims.op_Subtraction", "FStar.Math.Lemmas.lemma_mult_le_right", "FStar.Math.Lemmas.lemma_mod_lt" ]
[]
true
false
true
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.pow2_div_bound
val pow2_div_bound (#b: _) (n: UInt.uint_t b) (s: nat{s <= b}) : Lemma (n / pow2 s < pow2 (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)) = Math.lemma_div_lt n b s
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 540, "start_col": 0, "start_line": 538 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: FStar.UInt.uint_t b -> s: Prims.nat{s <= b} -> FStar.Pervasives.Lemma (ensures n / Prims.pow2 s < Prims.pow2 (b - s))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.u64_pow2_bound
val u64_pow2_bound (a: UInt.uint_t 64) (s: nat) : Lemma (a * pow2 s < pow2 (64 + s))
val u64_pow2_bound (a: UInt.uint_t 64) (s: nat) : Lemma (a * pow2 s < pow2 (64 + s))
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 651, "start_col": 0, "start_line": 648 }
(* 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')) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt.uint_t 64 -> s: Prims.nat -> FStar.Pervasives.Lemma (ensures a * Prims.pow2 s < Prims.pow2 (64 + s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt.uint_t", "Prims.nat", "FStar.Math.Lemmas.lemma_mult_le_right", "Prims.pow2", "Prims.unit", "FStar.Math.Lemmas.pow2_plus", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_LessThan", "FStar.Mul.op_Star", "Prims.op_Addition", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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)
false
FStar.UInt128.fst
FStar.UInt128.div_plus_multiple
val div_plus_multiple (a b: nat) (k: pos) : Lemma (requires (a < k)) (ensures ((a + b * k) / k == b))
val div_plus_multiple (a b: nat) (k: pos) : Lemma (requires (a < k)) (ensures ((a + b * k) / k == b))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 566, "start_col": 0, "start_line": 562 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Prims.nat -> b: Prims.nat -> k: Prims.pos -> FStar.Pervasives.Lemma (requires a < k) (ensures (a + b * k) / k == b)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
let div_plus_multiple (a 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
false
FStar.UInt128.fst
FStar.UInt128.add_mod_small
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)))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 585, "start_col": 0, "start_line": 582 }
(* 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) ==
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> m: Prims.nat -> k1: Prims.pos -> k2: Prims.pos -> FStar.Pervasives.Lemma (requires n < k1) (ensures n + k1 * m % (k1 * k2) == (n + k1 * m) % (k1 * k2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.pos", "FStar.UInt128.div_add_small", "Prims.unit", "FStar.UInt128.mod_spec", "Prims.op_Addition", "FStar.Mul.op_Star" ]
[]
true
false
true
false
false
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
FStar.UInt128.fst
FStar.UInt128.mul_mod_bound
val mul_mod_bound : n:nat -> s1:nat -> s2:nat{s2>=s1} -> Lemma (n * pow2 s1 % pow2 s2 <= pow2 s2 - pow2 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 = // 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 641, "start_col": 0, "start_line": 633 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 5, "quake_keep": false, "quake_lo": 1, "retry": true, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> 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)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
false
let mul_mod_bound n s1 s2 =
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
false
FStar.UInt128.fst
FStar.UInt128.add_u64_shift_left
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)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 558, "start_col": 0, "start_line": 543 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "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" }
false
hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt64.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_t_mod_val
val 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)
val 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 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); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 674, "start_col": 0, "start_line": 666 }
(* 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; ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: Prims.nat{s < 64} -> FStar.Pervasives.Lemma (ensures FStar.UInt128.v a * Prims.pow2 s % Prims.pow2 128 == FStar.UInt64.v (Mkuint128?.low a) * Prims.pow2 s + (FStar.UInt64.v (Mkuint128?.high a) * Prims.pow2 64) * Prims.pow2 s % Prims.pow2 128)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt128.t", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Prims.unit", "FStar.Math.Lemmas.paren_mul_right", "Prims.pow2", "FStar.Math.Lemmas.pow2_plus", "FStar.UInt128.shift_t_mod_val'", "FStar.UInt.uint_t", "FStar.UInt64.v", "FStar.UInt128.__proj__Mkuint128__item__high", "FStar.UInt128.__proj__Mkuint128__item__low", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.int", "Prims.op_Modulus", "FStar.Mul.op_Star", "FStar.UInt128.v", "Prims.op_Addition", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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); ()
false
FStar.UInt128.fst
FStar.UInt128.shift_left_small
val 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))
val 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))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 689, "start_col": 0, "start_line": 677 }
(* 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); ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "FStar.UInt128.t", "FStar.UInt32.t", "FStar.UInt32.eq", "FStar.UInt32.__uint_to_t", "Prims.bool", "Prims.unit", "FStar.UInt128.shift_t_mod_val", "FStar.UInt128.mod_spec_rew_n", "FStar.Mul.op_Star", "Prims.pow2", "FStar.UInt.uint_t", "FStar.UInt64.v", "FStar.UInt128.__proj__Mkuint128__item__high", "FStar.UInt128.__proj__Mkuint128__item__low", "FStar.UInt32.v", "FStar.UInt128.uint128", "FStar.UInt128.Mkuint128", "FStar.UInt64.shift_left", "FStar.UInt128.add_u64_shift_left_respec", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Equality", "Prims.int", "FStar.UInt128.v", "Prims.op_Modulus" ]
[]
false
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.mod_then_mul_64
val mod_then_mul_64 (n: nat) : Lemma ((n % pow2 64) * pow2 64 == n * pow2 64 % pow2 128)
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) = Math.pow2_plus 64 64; mod_mul n (pow2 64) (pow2 64)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 589, "start_col": 0, "start_line": 587 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> FStar.Pervasives.Lemma (ensures (n % Prims.pow2 64) * Prims.pow2 64 == n * Prims.pow2 64 % Prims.pow2 128)
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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)
false
FStar.UInt128.fst
FStar.UInt128.add_u64_shift_left_respec
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))
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)) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 612, "start_col": 0, "start_line": 593 }
(* 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) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt64.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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 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
false
FStar.UInt128.fst
FStar.UInt128.shift_left_large
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}
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}
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 706, "start_col": 0, "start_line": 696 }
(* 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}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 5, "quake_keep": false, "quake_lo": 1, "retry": true, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t{FStar.UInt32.v s >= 64 /\ FStar.UInt32.v s < 128} -> r: FStar.UInt128.t {FStar.UInt128.v r = FStar.UInt128.v a * Prims.pow2 (FStar.UInt32.v s) % Prims.pow2 128}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt128.t", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt32.v", "Prims.op_LessThan", "Prims.unit", "FStar.UInt128.shift_left_large_lemma_t", "Prims._assert", "Prims.eq2", "Prims.int", "FStar.Mul.op_Star", "FStar.UInt64.v", "FStar.UInt128.__proj__Mkuint128__item__high", "Prims.pow2", "Prims.op_Modulus", "FStar.UInt128.__proj__Mkuint128__item__low", "FStar.Math.Lemmas.pow2_plus", "Prims.op_Subtraction", "FStar.UInt128.mod_mul", "FStar.UInt128.uint128", "FStar.UInt128.Mkuint128", "FStar.UInt64.uint_to_t", "FStar.UInt64.shift_left", "FStar.UInt32.sub", "FStar.UInt128.u32_64", "Prims.op_Equality", "FStar.UInt128.v" ]
[]
false
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.add_mod
val add_mod: a:t -> b:t -> Pure t (requires True) (ensures (fun c -> (v a + v b) % pow2 n = v c))
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)) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 282, "start_col": 0, "start_line": 240 }
(* 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.
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 5, "quake_keep": false, "quake_lo": 1, "retry": true, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.add_u64_shift_right
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))
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)) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 728, "start_col": 0, "start_line": 713 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt64.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.u128_div_pow2
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))
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))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 84, "end_line": 771, "start_col": 0, "start_line": 768 }
(* 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}) :
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: Prims.nat{s < 64} -> 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))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt128.t", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.Math.Lemmas.division_addition_lemma", "FStar.UInt64.v", "FStar.UInt128.__proj__Mkuint128__item__low", "Prims.pow2", "FStar.Mul.op_Star", "FStar.UInt128.__proj__Mkuint128__item__high", "Prims.op_Subtraction", "Prims.unit", "FStar.Math.Lemmas.paren_mul_right", "FStar.Math.Lemmas.pow2_plus" ]
[]
true
false
true
false
false
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))
false
FStar.UInt128.fst
FStar.UInt128.div_add_small
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)))
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 576, "start_col": 0, "start_line": 571 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> m: Prims.nat -> k1: Prims.pos -> k2: Prims.pos -> FStar.Pervasives.Lemma (requires n < k1) (ensures k1 * m / (k1 * k2) == (n + k1 * m) / (k1 * k2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.pos", "FStar.UInt128.div_plus_multiple", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.int", "Prims.op_Division", "FStar.Mul.op_Star", "FStar.UInt128.mul_div_cancel", "FStar.UInt128.div_product", "Prims.op_Addition" ]
[]
true
false
true
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_right_reconstruct
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)
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); ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 764, "start_col": 0, "start_line": 758 }
(* 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} ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a_h: FStar.UInt.uint_t 64 -> s: Prims.nat{s < 64} -> FStar.Pervasives.Lemma (ensures a_h * Prims.pow2 (64 - s) == (a_h / Prims.pow2 s) * Prims.pow2 64 + a_h * Prims.pow2 64 / Prims.pow2 s % Prims.pow2 64)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt.uint_t", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.int", "FStar.Mul.op_Star", "Prims.op_Division", "Prims.pow2", "FStar.UInt128.mul_div_cancel", "FStar.UInt128.div_product_comm", "FStar.UInt128.mod_spec_rew_n", "Prims.op_Subtraction", "FStar.UInt128.mul_pow2_diff" ]
[]
true
false
true
false
false
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); ()
false
FStar.UInt128.fst
FStar.UInt128.shift_left
val shift_left: a:t -> s:UInt32.t -> Pure t (requires (U32.v s < n)) (ensures (fun c -> v c = ((v a * pow2 (UInt32.v s)) % pow2 n)))
val shift_left: a:t -> s:UInt32.t -> Pure t (requires (U32.v s < n)) (ensures (fun c -> v c = ((v a * pow2 (UInt32.v s)) % pow2 n)))
let shift_left a s = if (U32.lt s u32_64) then shift_left_small a s else shift_left_large a s
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 711, "start_col": 0, "start_line": 709 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "FStar.UInt128.t", "FStar.UInt32.t", "FStar.UInt32.lt", "FStar.UInt128.u32_64", "FStar.UInt128.shift_left_small", "Prims.bool", "FStar.UInt128.shift_left_large" ]
[]
false
false
false
false
false
let shift_left a s =
if (U32.lt s u32_64) then shift_left_small a s else shift_left_large a s
false
FStar.UInt128.fst
FStar.UInt128.div_product_comm
val div_product_comm : n1:nat -> k1:pos -> k2:pos -> Lemma (n1 / k1 / k2 == 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 = div_product n1 k1 k2; div_product n1 k2 k1
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 754, "start_col": 0, "start_line": 752 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n1: Prims.nat -> k1: Prims.pos -> k2: Prims.pos -> FStar.Pervasives.Lemma (ensures n1 / k1 / k2 == n1 / k2 / k1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.nat", "Prims.pos", "FStar.UInt128.div_product", "Prims.unit" ]
[]
true
false
true
false
false
let div_product_comm n1 k1 k2 =
div_product n1 k1 k2; div_product n1 k2 k1
false
FStar.UInt128.fst
FStar.UInt128.shift_right_small
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)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 786, "start_col": 0, "start_line": 773 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.add_u64_shift_right_respec
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))
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)) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 744, "start_col": 0, "start_line": 737 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
hi: FStar.UInt64.t -> lo: FStar.UInt64.t -> s: FStar.UInt32.t{FStar.UInt32.v s < 64} -> Prims.Pure FStar.UInt64.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_right
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)))))
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))) = if U32.lt s u32_64 then shift_right_small a s else shift_right_large a s
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 30, "end_line": 805, "start_col": 0, "start_line": 800 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_right_large
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)))
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))) = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 798, "start_col": 0, "start_line": 788 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> s: FStar.UInt32.t{FStar.UInt32.v s >= 64 /\ FStar.UInt32.v s < 128} -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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
false
FStar.UInt128.fst
FStar.UInt128.gte
val gte (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == gte #n (v a) (v b)))
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) = U64.gt a.high b.high || (U64.eq a.high b.high && U64.gte a.low b.low)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 63, "end_line": 813, "start_col": 0, "start_line": 812 }
(* 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 ||
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
false
let gte (a b: t) =
U64.gt a.high b.high || (U64.eq a.high b.high && U64.gte a.low b.low)
false
FStar.UInt128.fst
FStar.UInt128.gt
val gt (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == gt #n (v a) (v b)))
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) = U64.gt a.high b.high || (U64.eq a.high b.high && U64.gt a.low b.low)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 809, "start_col": 0, "start_line": 808 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
false
let gt (a b: t) =
U64.gt a.high b.high || (U64.eq a.high b.high && U64.gt a.low b.low)
false
FStar.UInt128.fst
FStar.UInt128.lt
val lt (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == lt #n (v a) (v b)))
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) = U64.lt a.high b.high || (U64.eq a.high b.high && U64.lt a.low b.low)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 811, "start_col": 0, "start_line": 810 }
(* 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 ||
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
false
let lt (a b: t) =
U64.lt a.high b.high || (U64.eq a.high b.high && U64.lt a.low b.low)
false
FStar.UInt128.fst
FStar.UInt128.pll_l
val pll_l (x y: U64.t) : UInt.uint_t 32
val pll_l (x y: U64.t) : UInt.uint_t 32
let pll_l (x y: U64.t) : UInt.uint_t 32 = l32 (pll x y)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 986, "start_col": 0, "start_line": 985 }
(* 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))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> y: FStar.UInt64.t -> FStar.UInt.uint_t 32
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.UInt128.l32", "FStar.UInt128.pll", "FStar.UInt.uint_t" ]
[]
false
false
false
false
false
let pll_l (x y: U64.t) : UInt.uint_t 32 =
l32 (pll x y)
false
FStar.UInt128.fst
FStar.UInt128.pll_h
val pll_h (x y: U64.t) : UInt.uint_t 32
val pll_h (x y: U64.t) : UInt.uint_t 32
let pll_h (x y: U64.t) : UInt.uint_t 32 = h32 (pll x y)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 988, "start_col": 0, "start_line": 987 }
(* 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 =
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> y: FStar.UInt64.t -> FStar.UInt.uint_t 32
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.UInt128.h32", "FStar.UInt128.pll", "FStar.UInt.uint_t" ]
[]
false
false
false
false
false
let pll_h (x y: U64.t) : UInt.uint_t 32 =
h32 (pll x y)
false
FStar.UInt128.fst
FStar.UInt128.u64_logand_comm
val u64_logand_comm (a b: U64.t) : Lemma (U64.logand a b == U64.logand b a)
val u64_logand_comm (a b: U64.t) : Lemma (U64.logand a b == U64.logand b a)
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 818, "start_col": 0, "start_line": 817 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.logand a b == FStar.UInt64.logand b a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logand_commutative", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.UInt64.logand", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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)
false
FStar.UInt128.fst
FStar.UInt128.u64_and_0
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)]
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 823, "start_col": 0, "start_line": 823 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> 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)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logand_lemma_1", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit" ]
[]
true
false
true
false
false
let u64_and_0 a b =
UInt.logand_lemma_1 (U64.v a)
false
FStar.UInt128.fst
FStar.UInt128.eq
val eq (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == eq #n (v a) (v b)))
val eq (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == eq #n (v a) (v b)))
let eq (a b:t) = U64.eq a.low b.low && U64.eq a.high b.high
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 807, "start_col": 0, "start_line": 807 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool
Prims.Pure
[]
[]
[ "FStar.UInt128.t", "Prims.op_AmpAmp", "FStar.UInt64.eq", "FStar.UInt128.__proj__Mkuint128__item__low", "FStar.UInt128.__proj__Mkuint128__item__high", "Prims.bool" ]
[]
false
false
false
false
false
let eq (a b: t) =
U64.eq a.low b.low && U64.eq a.high b.high
false
FStar.UInt128.fst
FStar.UInt128.u64_0_and
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)]
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)] = u64_logand_comm a b
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 828, "start_col": 0, "start_line": 825 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> 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)]
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.shift_t_val
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))
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)) = Math.pow2_plus 64 s; ()
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 626, "start_col": 0, "start_line": 623 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 5, "quake_keep": false, "quake_lo": 1, "retry": true, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
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))
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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; ()
false
FStar.UInt128.fst
FStar.UInt128.u64_or_1
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)]
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 859, "start_col": 0, "start_line": 859 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> 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)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logor_lemma_2", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit" ]
[]
true
false
true
false
false
let u64_or_1 a b =
UInt.logor_lemma_2 (U64.v a)
false
FStar.UInt128.fst
FStar.UInt128.u64_not_1
val u64_not_1 (a:U64.t) : Lemma (U64.v a = pow2 64 - 1 ==> U64.v (U64.lognot a) = 0) [SMTPat (U64.lognot a)]
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 64, "end_line": 880, "start_col": 0, "start_line": 879 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.v a = Prims.pow2 64 - 1 ==> FStar.UInt64.v (FStar.UInt64.lognot a) = 0) [SMTPat (FStar.UInt64.lognot a)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.nth_lemma", "FStar.UInt.lognot", "FStar.UInt.ones", "FStar.UInt.zero", "Prims.unit" ]
[]
true
false
true
false
false
let u64_not_1 a =
UInt.nth_lemma (UInt.lognot #64 (UInt.ones 64)) (UInt.zero 64)
false
FStar.UInt128.fst
FStar.UInt128.lte
val lte (a:t) (b:t) : Pure bool (requires True) (ensures (fun r -> r == lte #n (v a) (v b)))
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) = U64.lt a.high b.high || (U64.eq a.high b.high && U64.lte a.low b.low)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 63, "end_line": 815, "start_col": 0, "start_line": 814 }
(* 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 ||
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> Prims.Pure Prims.bool
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
false
let lte (a b: t) =
U64.lt a.high b.high || (U64.eq a.high b.high && U64.lte a.low b.low)
false
FStar.UInt128.fst
FStar.UInt128.u64_1s_and
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)]
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 834, "start_col": 0, "start_line": 834 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.v a = Prims.pow2 64 - 1 /\ FStar.UInt64.v b = Prims.pow2 64 - 1 ==> FStar.UInt64.v (FStar.UInt64.logand a b) = Prims.pow2 64 - 1) [SMTPat (FStar.UInt64.logand a b)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logand_lemma_2", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit" ]
[]
true
false
true
false
false
let u64_1s_and a b =
UInt.logand_lemma_2 (U64.v a)
false
FStar.UInt128.fst
FStar.UInt128.u64_not_0
val u64_not_0 (a:U64.t) : Lemma (U64.v a = 0 ==> U64.v (U64.lognot a) = pow2 64 - 1) [SMTPat (U64.lognot 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 874, "start_col": 0, "start_line": 874 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.v a = 0 ==> FStar.UInt64.v (FStar.UInt64.lognot a) = Prims.pow2 64 - 1) [SMTPat (FStar.UInt64.lognot a)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.lognot_lemma_1", "Prims.unit" ]
[]
true
false
true
false
false
let u64_not_0 a =
UInt.lognot_lemma_1 #64
false
FStar.UInt128.fst
FStar.UInt128.u64_or_0
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)]
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 869, "start_col": 0, "start_line": 869 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.v a = 0 /\ FStar.UInt64.v b = 0 ==> FStar.UInt64.v (FStar.UInt64.logor a b) = 0 ) [SMTPat (FStar.UInt64.logor a b)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logor_lemma_1", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit" ]
[]
true
false
true
false
false
let u64_or_0 a b =
UInt.logor_lemma_1 (U64.v a)
false
FStar.UInt128.fst
FStar.UInt128.u64_1_or
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)]
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)] = u64_logor_comm a b
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 20, "end_line": 864, "start_col": 0, "start_line": 861 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> 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)]
FStar.Pervasives.Lemma
[ "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" ]
[]
true
false
true
false
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
false
FStar.UInt128.fst
FStar.UInt128.uint128_to_uint64
val uint128_to_uint64: a:t -> b:U64.t{U64.v b == v a % pow2 64}
val uint128_to_uint64: a:t -> b:U64.t{U64.v b == v a % pow2 64}
let uint128_to_uint64 (a:t) : b:U64.t{U64.v b == v a % pow2 64} = a.low
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 71, "end_line": 896, "start_col": 0, "start_line": 896 }
(* 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; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt64.t{FStar.UInt64.v b == FStar.UInt128.v a % Prims.pow2 64}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt128.t", "FStar.UInt128.__proj__Mkuint128__item__low", "FStar.UInt64.t", "Prims.eq2", "Prims.int", "FStar.UInt64.v", "Prims.op_Modulus", "FStar.UInt128.v", "Prims.pow2" ]
[]
false
false
false
false
false
let uint128_to_uint64 (a: t) : b: U64.t{U64.v b == v a % pow2 64} =
a.low
false
FStar.UInt128.fst
FStar.UInt128.u64_logor_comm
val u64_logor_comm (a b: U64.t) : Lemma (U64.logor a b == U64.logor b a)
val u64_logor_comm (a b: U64.t) : Lemma (U64.logor a b == U64.logor b a)
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)
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 854, "start_col": 0, "start_line": 853 }
(* 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)) = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt64.t -> FStar.Pervasives.Lemma (ensures FStar.UInt64.logor a b == FStar.UInt64.logor b a)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.UInt64.t", "FStar.UInt.logor_commutative", "FStar.UInt64.n", "FStar.UInt64.v", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.UInt64.logor", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
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)
false
FStar.UInt128.fst
FStar.UInt128.eq_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)})
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))) = let mask = U64.logand (U64.eq_mask a.low b.low) (U64.eq_mask a.high b.high) in { low = mask; high = mask; }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 30, "end_line": 841, "start_col": 0, "start_line": 836 }
(* 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)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt128.t -> b: FStar.UInt128.t -> c: FStar.UInt128.t { (FStar.UInt128.v a = FStar.UInt128.v b ==> FStar.UInt128.v c = Prims.pow2 FStar.UInt128.n - 1) /\ (FStar.UInt128.v a <> FStar.UInt128.v b ==> FStar.UInt128.v c = 0) }
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt128.t", "FStar.UInt128.Mkuint128", "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" ]
[]
false
false
false
false
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 }
false
FStar.UInt128.fst
FStar.UInt128.l32
val l32 (x: UInt.uint_t 64) : UInt.uint_t 32
val l32 (x: UInt.uint_t 64) : UInt.uint_t 32
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x % pow2 32
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 967, "start_col": 0, "start_line": 967 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt.uint_t 64 -> FStar.UInt.uint_t 32
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt.uint_t", "Prims.op_Modulus", "Prims.pow2" ]
[]
false
false
false
false
false
let l32 (x: UInt.uint_t 64) : UInt.uint_t 32 =
x % pow2 32
false
FStar.UInt128.fst
FStar.UInt128.phh
val phh (x y: U64.t) : n: UInt.uint_t 64 {n < pow2 64 - pow2 32 - 1}
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} = mul32_bound (h32 (U64.v x)) (h32 (U64.v y))
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 983, "start_col": 0, "start_line": 982 }
(* 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} =
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> y: FStar.UInt64.t -> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.UInt128.mul32_bound", "FStar.UInt128.h32", "FStar.UInt64.v", "FStar.UInt.uint_t", "Prims.b2t", "Prims.op_LessThan", "Prims.op_Subtraction", "Prims.pow2" ]
[]
false
false
false
false
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))
false
FStar.UInt128.fst
FStar.UInt128.h32
val h32 (x: UInt.uint_t 64) : UInt.uint_t 32
val h32 (x: UInt.uint_t 64) : UInt.uint_t 32
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 = x / pow2 32
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 968, "start_col": 0, "start_line": 968 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt.uint_t 64 -> FStar.UInt.uint_t 32
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt.uint_t", "Prims.op_Division", "Prims.pow2" ]
[]
false
false
false
false
false
let h32 (x: UInt.uint_t 64) : UInt.uint_t 32 =
x / pow2 32
false
FStar.UInt128.fst
FStar.UInt128.mul32
val mul32: x:U64.t -> y:U32.t -> Pure t (requires True) (ensures (fun r -> v r == U64.v x * U32.v y))
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 = 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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 965, "start_col": 0, "start_line": 940 }
(* 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
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt64.t -> y: FStar.UInt32.t -> Prims.Pure FStar.UInt128.t
Prims.Pure
[]
[]
[ "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" ]
[]
false
false
false
false
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); 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
false
FStar.UInt128.fst
FStar.UInt128.uint64_to_uint128
val uint64_to_uint128: a:U64.t -> b:t{v b == U64.v a}
val uint64_to_uint128: a:U64.t -> b:t{v b == U64.v a}
let uint64_to_uint128 (a:U64.t) = { low = a; high = U64.uint_to_t 0; }
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 70, "end_line": 894, "start_col": 0, "start_line": 894 }
(* 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; }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.UInt64.t -> b: FStar.UInt128.t{FStar.UInt128.v b == FStar.UInt64.v a}
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt64.t", "FStar.UInt128.Mkuint128", "FStar.UInt64.uint_to_t", "FStar.UInt128.t", "Prims.eq2", "Prims.int", "Prims.l_or", "FStar.UInt.size", "FStar.UInt128.n", "FStar.UInt64.n", "FStar.UInt128.v", "FStar.UInt64.v" ]
[]
false
false
false
false
false
let uint64_to_uint128 (a: U64.t) =
{ low = a; high = U64.uint_to_t 0 }
false
FStar.UInt128.fst
FStar.UInt128.mul32_bound
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}
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
{ "file_name": "ulib/FStar.UInt128.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 7, "end_line": 974, "start_col": 0, "start_line": 972 }
(* 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 ->
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.UInt.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Tactics.BV.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.Int.Cast.fst.checked", "FStar.Classical.Sugar.fsti.checked", "FStar.Calc.fsti.checked", "FStar.BV.fsti.checked", "FStar.BitVector.fst.checked" ], "interface_file": true, "source_file": "FStar.UInt128.fst" }
[ { "abbrev": true, "full_module": "FStar.Tactics.BV", "short_module": "TBV" }, { "abbrev": true, "full_module": "FStar.Tactics.V2", "short_module": "T" }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.BV", "short_module": null }, { "abbrev": true, "full_module": "FStar.Math.Lemmas", "short_module": "Math" }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.BitVector", "short_module": "BV" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt", "short_module": "UInt" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.UInt", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt.uint_t 32 -> y: FStar.UInt.uint_t 32 -> n: FStar.UInt.uint_t 64 {n < Prims.pow2 64 - Prims.pow2 32 - 1 /\ n == x * y}
Prims.Tot
[ "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" ]
[]
false
false
false
false
false
let mul32_bound x y =
u32_product_bound x y; x * y
false